class Prismic::LruCache

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)`

Attributes

intern[R]

@return [LRUHash<String,Object>]

Public Class Methods

new(max_size=100) click to toggle source

@param max_size [Fixnum] (100) The default maximum of keys to store

# File lib/prismic/cache/lru.rb, line 22
def initialize(max_size=100)
  @intern = Hashery::LRUHash.new(max_size)
end

Public Instance Methods

[](key)
Alias for: get
[]=(key, value) click to toggle source
# File lib/prismic/cache/lru.rb, line 37
def []=(key, value)
  set(key, value, nil)
end
clear!()
Alias for: invalidate_all!
delete(key) click to toggle source
# File lib/prismic/cache/lru.rb, line 60
def delete(key)
  @intern.delete(key)
  nil
end
expired?(key) click to toggle source
# 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) click to toggle source

Get a cache entry

@param key [String] The key to fetch

@return [Object] The cache object as was stored

# File lib/prismic/cache/lru.rb, line 46
def get(key)
  return delete(key) if expired?(key)
  include?(key) ? @intern[key][:data] : nil
end
Also aliased as: []
get_or_set(key, value = nil, expired_in = nil) { |: value, expired_in)| ... } click to toggle source
# 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) click to toggle source

Checks if a cache entry exists

@param key [String] The key to test

@return [Boolean]

# File lib/prismic/cache/lru.rb, line 70
def has_key?(key)
  @intern.has_key?(key)
end
Also aliased as: include?
include?(key)
Alias for: has_key?
invalidate_all!() click to toggle source

Invalidates all entries

# File lib/prismic/cache/lru.rb, line 85
def invalidate_all!
  @intern.clear
end
Also aliased as: clear!
keys() click to toggle source

Expose the Hash keys

This is only for debugging purposes.

@return [Array<String>]

# File lib/prismic/cache/lru.rb, line 95
def keys
  @intern.keys
end
length()
Alias for: size
set(key, value, expired_in = nil) click to toggle source

Add a cache entry.

@param key [String] The key @param value [Object] The value to store

@return [Object] The stored value

# 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() click to toggle source

Return the number of stored keys

@return [Fixum]

# File lib/prismic/cache/lru.rb, line 102
def size
  @intern.size
end
Also aliased as: length