Class: CasheDevice::CacheDeviceBase
- Inherits:
-
Object
- Object
- CasheDevice::CacheDeviceBase
- Includes:
- Singleton
- Defined in:
- lib/cache.rb
Direct Known Subclasses
BBCNet::CacheMetaInfoDevice, CacheHttpDiskDevice, CacheRssDevice
Defined Under Namespace
Classes: CachedData
Instance Attribute Summary collapse
-
#cacheDuration ⇒ Object
Returns the value of attribute cacheDuration.
-
#cacheMax ⇒ Object
Returns the value of attribute cacheMax.
Class Method Summary collapse
Instance Method Summary collapse
-
#directRead(url) ⇒ Object
: data, key key : key to restore data.
-
#initialize(cacheDuration = 26*60, cacheMax = 10) ⇒ CacheDeviceBase
constructor
A new instance of CacheDeviceBase.
- #read(url) ⇒ Object
-
#restoreCache(key) ⇒ Object
: data restore data from key.
Constructor Details
#initialize(cacheDuration = 26*60, cacheMax = 10) ⇒ CacheDeviceBase
Returns a new instance of CacheDeviceBase.
13 14 15 16 17 18 |
# File 'lib/cache.rb', line 13 def initialize(cacheDuration = 26*60, cacheMax=10) @cacheDuration = cacheDuration @cache = Hash.new @cacheLRU = [] # Least Recently Used @cacheMax = cacheMax end |
Instance Attribute Details
#cacheDuration ⇒ Object
Returns the value of attribute cacheDuration.
12 13 14 |
# File 'lib/cache.rb', line 12 def cacheDuration @cacheDuration end |
#cacheMax ⇒ Object
Returns the value of attribute cacheMax.
12 13 14 |
# File 'lib/cache.rb', line 12 def cacheMax @cacheMax end |
Class Method Details
.read(url) ⇒ Object
59 60 61 |
# File 'lib/cache.rb', line 59 def self.read(url) self.instance.read(url) end |
Instance Method Details
#directRead(url) ⇒ Object
Returns : data, key key : key to restore data.
22 23 24 |
# File 'lib/cache.rb', line 22 def directRead(url) raise "Implement directRead method." end |
#read(url) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cache.rb', line 32 def read(url) startTime = Time.now cached = @cache[url] if cached and cached.expireTime > startTime then @cacheLRU.delete(cached) @cacheLRU.push(cached) data = restoreCache(cached.key) $log.debug { "cached %s: Time %f sec" % [self.class.name, (Time.now - startTime).to_f] } return data end if @cacheLRU.size >= @cacheMax then oldest = @cacheLRU.shift @cache.delete(oldest) end cached = CachedData.new cached.url = url cached.expireTime = startTime + @cacheDuration data, cached.key = directRead(url) @cache[url] = cached @cacheLRU.push(cached) $log.debug {"direct read %s: Time %f sec" % [self.class.name, (Time.now - startTime).to_f] } data end |
#restoreCache(key) ⇒ Object
Returns : data restore data from key.
28 29 30 |
# File 'lib/cache.rb', line 28 def restoreCache(key) key end |