Class: Synthra::APIServer::Cache
- Inherits:
-
Object
- Object
- Synthra::APIServer::Cache
- Defined in:
- lib/synthra/api_server.rb
Overview
Simple cache
Instance Method Summary collapse
- #get(key) ⇒ Object
-
#initialize(ttl) ⇒ Cache
constructor
A new instance of Cache.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(ttl) ⇒ Cache
Returns a new instance of Cache.
556 557 558 559 560 |
# File 'lib/synthra/api_server.rb', line 556 def initialize(ttl) @ttl = ttl @store = {} @mutex = Mutex.new end |
Instance Method Details
#get(key) ⇒ Object
562 563 564 565 566 567 568 569 |
# File 'lib/synthra/api_server.rb', line 562 def get(key) @mutex.synchronize do entry = @store[key] return nil unless entry return nil if Time.now > entry[:expires_at] entry[:value] end end |
#set(key, value) ⇒ Object
571 572 573 574 575 |
# File 'lib/synthra/api_server.rb', line 571 def set(key, value) @mutex.synchronize do @store[key] = { value: value, expires_at: Time.now + @ttl } end end |