Class: Prismic::LruCache
- Inherits:
 - 
      Object
      
        
- Object
 - Prismic::LruCache
 
 
- Defined in:
 - lib/prismic/cache/lru.rb
 
Overview
This is a simple cache class provided with the prismic.io Ruby kit.
It is pretty dumb but effective: * everything is stored in memory,
If you need a smarter caching (for instance, rely on memcached), you can
extend this class and replace its methods, and when creating your API object
like this for instance: Prismic.api(url, options), pass the name of the
class you created as a :cache option. Therefore, to use this simple cache,
you can create your API object like this: Prismic.api(url, cache:
Prismic::DefaultCache)
Instance Attribute Summary collapse
- #intern ⇒ LRUHash<String,Object> readonly
 
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
 - #delete(key) ⇒ Object
 - #expired?(key) ⇒ Boolean
 - 
  
    
      #get(key)  ⇒ Object 
    
    
      (also: #[])
    
  
  
  
  
  
  
  
  
  
    
Get a cache entry.
 - #get_or_set(key, value = nil, expired_in = nil) ⇒ Object
 - 
  
    
      #has_key?(key)  ⇒ Boolean 
    
    
      (also: #include?)
    
  
  
  
  
  
  
  
  
  
    
Checks if a cache entry exists.
 - 
  
    
      #initialize(max_size = 100)  ⇒ LruCache 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of LruCache.
 - 
  
    
      #invalidate_all!  ⇒ Object 
    
    
      (also: #clear!)
    
  
  
  
  
  
  
  
  
  
    
Invalidates all entries.
 - 
  
    
      #keys  ⇒ Array<String> 
    
    
  
  
  
  
  
  
  
  
  
    
Expose the Hash keys.
 - 
  
    
      #set(key, value, expired_in = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Add a cache entry.
 - 
  
    
      #size  ⇒ Fixum 
    
    
      (also: #length)
    
  
  
  
  
  
  
  
  
  
    
Return the number of stored keys.
 
Constructor Details
#initialize(max_size = 100) ⇒ LruCache
Returns a new instance of LruCache
      22 23 24  | 
    
      # File 'lib/prismic/cache/lru.rb', line 22 def initialize(max_size=100) @intern = Hashery::LRUHash.new(max_size) end  | 
  
Instance Attribute Details
#intern ⇒ LRUHash<String,Object> (readonly)
      19 20 21  | 
    
      # File 'lib/prismic/cache/lru.rb', line 19 def intern @intern end  | 
  
Instance Method Details
#[]=(key, value) ⇒ Object
      37 38 39  | 
    
      # File 'lib/prismic/cache/lru.rb', line 37 def []=(key, value) set(key, value, nil) end  | 
  
#delete(key) ⇒ Object
      60 61 62 63  | 
    
      # File 'lib/prismic/cache/lru.rb', line 60 def delete(key) @intern.delete(key) nil end  | 
  
#expired?(key) ⇒ Boolean
      75 76 77 78 79 80 81 82  | 
    
      # File 'lib/prismic/cache/lru.rb', line 75 def expired?(key) if include?(key) && @intern[key][:expired_in] != nil expired_in = @intern[key][:expired_in] expired_in && expired_in < Time.now.getutc.to_i else false end end  | 
  
#get(key) ⇒ Object Also known as: []
Get a cache entry
      46 47 48 49  | 
    
      # File 'lib/prismic/cache/lru.rb', line 46 def get(key) return delete(key) if expired?(key) include?(key) ? @intern[key][:data] : nil end  | 
  
#get_or_set(key, value = nil, expired_in = nil) ⇒ Object
      52 53 54 55 56 57 58  | 
    
      # File 'lib/prismic/cache/lru.rb', line 52 def get_or_set(key, value = nil, expired_in = nil) if include?(key) && !expired?(key) return get(key) else set(key, block_given? ? yield : value, expired_in) end end  | 
  
#has_key?(key) ⇒ Boolean Also known as: include?
Checks if a cache entry exists
      70 71 72  | 
    
      # File 'lib/prismic/cache/lru.rb', line 70 def has_key?(key) @intern.has_key?(key) end  | 
  
#invalidate_all! ⇒ Object Also known as: clear!
Invalidates all entries
      85 86 87  | 
    
      # File 'lib/prismic/cache/lru.rb', line 85 def invalidate_all! @intern.clear end  | 
  
#keys ⇒ Array<String>
Expose the Hash keys
This is only for debugging purposes.
      95 96 97  | 
    
      # File 'lib/prismic/cache/lru.rb', line 95 def keys @intern.keys end  | 
  
#set(key, value, expired_in = nil) ⇒ Object
Add a cache entry.
      32 33 34 35  | 
    
      # File 'lib/prismic/cache/lru.rb', line 32 def set(key, value, expired_in = nil) @intern.store(key, { :data => value, :expired_in => expired_in = expired_in && Time.now.getutc.to_i + expired_in }) value end  | 
  
#size ⇒ Fixum Also known as: length
Return the number of stored keys
      102 103 104  | 
    
      # File 'lib/prismic/cache/lru.rb', line 102 def size @intern.size end  |