Class: SynCache::CacheEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/syncache/syncache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#syncObject (readonly)

use this to synchronize access to value



35
36
37
# File 'lib/syncache/syncache.rb', line 35

def sync
  @sync
end

#ttlObject

change this to make the entry expire sooner



32
33
34
# File 'lib/syncache/syncache.rb', line 32

def ttl
  @ttl
end

#valueObject

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_accessObject

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_indexObject

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/syncache/syncache.rb', line 52

def stale?
  @expires < Time.now
end