Class: SynCache::CacheEntry
- Inherits:
-
Object
- Object
- SynCache::CacheEntry
- Defined in:
- lib/syncache/syncache.rb
Instance Attribute Summary collapse
-
#sync ⇒ Object
readonly
use this to synchronize access to
value
. -
#ttl ⇒ Object
change this to make the entry expire sooner.
-
#value ⇒ Object
stores the value object.
Instance Method Summary collapse
-
#expire_at(time) ⇒ Object
mark entry as dirty and schedule it to expire at given time.
-
#initialize(ttl = nil, value = nil) ⇒ CacheEntry
constructor
A new instance of CacheEntry.
-
#record_access ⇒ Object
record the fact that the entry was accessed.
-
#replacement_index ⇒ Object
entries with lowest index will be replaced first.
-
#stale? ⇒ Boolean
check if entry is stale.
Constructor Details
#initialize(ttl = nil, value = nil) ⇒ CacheEntry
Returns a new instance of CacheEntry.
19 20 21 22 23 24 25 26 |
# File 'lib/syncache/syncache.rb', line 19 def initialize(ttl = nil, value = nil) @value = value @ttl = ttl @dirty = false record_access @sync = Mutex.new end |
Instance Attribute Details
#sync ⇒ Object (readonly)
use this to synchronize access to value
35 36 37 |
# File 'lib/syncache/syncache.rb', line 35 def sync @sync end |
#ttl ⇒ Object
change this to make the entry expire sooner
32 33 34 |
# File 'lib/syncache/syncache.rb', line 32 def ttl @ttl end |
#value ⇒ Object
stores the value object
29 30 31 |
# File 'lib/syncache/syncache.rb', line 29 def value @value end |
Instance Method Details
#expire_at(time) ⇒ Object
mark entry as dirty and schedule it to expire at given time
58 59 60 61 |
# File 'lib/syncache/syncache.rb', line 58 def expire_at(time) @expires = time if @expires > time @dirty = true end |
#record_access ⇒ Object
record the fact that the entry was accessed
39 40 41 42 |
# File 'lib/syncache/syncache.rb', line 39 def record_access return if @dirty @expires = Time.now + (@ttl or FOREVER) end |
#replacement_index ⇒ Object
entries with lowest index will be replaced first
46 47 48 |
# File 'lib/syncache/syncache.rb', line 46 def replacement_index @expires end |
#stale? ⇒ Boolean
check if entry is stale
52 53 54 |
# File 'lib/syncache/syncache.rb', line 52 def stale? @expires < Time.now end |