Class: Prismic::LruCache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 100) ⇒ LruCache

Returns a new instance of LruCache.

Parameters:

  • max_size (Fixnum) (defaults to: 100)

    (100) The default maximum of keys to store



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

#internLRUHash<String,Object> (readonly)

Returns:

  • (LRUHash<String,Object>)


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

Returns:

  • (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

Parameters:

  • key (String)

    The key to fetch

Returns:

  • (Object)

    The cache object as was stored



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

Parameters:

  • key (String)

    The key to test

Returns:

  • (Boolean)


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

#keysArray<String>

Expose the Hash keys

This is only for debugging purposes.

Returns:

  • (Array<String>)


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.

Parameters:

  • key (String)

    The key

  • value (Object)

    The value to store

Returns:

  • (Object)

    The stored value



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

#sizeFixum Also known as: length

Return the number of stored keys

Returns:

  • (Fixum)


102
103
104
# File 'lib/prismic/cache/lru.rb', line 102

def size
  @intern.size
end