Class: LocalCacheStore

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

Overview

This class is used to implement a local in memory cache store.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace = '') ⇒ LocalCacheStore

Returns a new instance of LocalCacheStore.



49
50
51
52
# File 'lib/cache_store.rb', line 49

def initialize(namespace = '')
  @store = Array.new
  @namespace = namespace
end

Instance Attribute Details

#storeObject

Returns the value of attribute store.



47
48
49
# File 'lib/cache_store.rb', line 47

def store
  @store
end

Instance Method Details

#exist?(key) ⇒ Boolean

This method is called to check if a value exists within this cache store for a specific key.

Parameters:

  • key (String)

    This is the unique key to reference the value to check for within this cache store.

Returns:

  • (Boolean)

    True or False to specify if the key exists in the cache store.



117
118
119
# File 'lib/cache_store.rb', line 117

def exist?(key)
  !@store.select { |i| i[:key] == build_key(key) }.empty?
end

#get(key, expires_in = 0, &block) ⇒ Object

This method is called to get a value from this cache store by it’s unique key.

Parameters:

  • key (String)

    This is the unique key to reference the value to fetch from within this cache store.

  • expires_in (Integer) (defaults to: 0)

    This is the number of seconds from the current time that this value should expire. (This is used in conjunction with the block to hydrate the cache key if it is empty.)

  • &block (Block)

    This block is provided to hydrate this cache store with the value for the request key when it is not found.

Returns:

  • (Object)

    The value for the specified unique key within the cache store.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cache_store.rb', line 76

def get(key, expires_in = 0, &block)

  #look for the cache item in the store
  items = @store.select { |i| i[:key] == build_key(key) }
  item = if !items.empty? then items[0] else nil end
  #check if a valid item was found in the store
  if item == nil || (item[:expires] != nil && item[:expires] <= Time.now.utc)
    #a valid item wasn't found so check if a hydration block was specified.
    if block_given?
      #create the item from the block
      value = yield
      #put the item in the store
      set(build_key(key), value, expires_in)
      return value
    else
      #no hydration block was specified

      #check if an expired item was found
      if item != nil
        #remove the expired item from the store
        remove(build_key(key))
      end
      return nil
    end
  end

  #return the item
  return item[:value]
end

#remove(key) ⇒ Object

This method is called to remove a value from this cache store by it’s unique key.

Parameters:

  • key (String)

    This is the unique key to reference the value to remove from this cache store.



109
110
111
# File 'lib/cache_store.rb', line 109

def remove(key)
  @store.delete_if { |i| i[:key] == build_key(key) }
end

#set(key, value, expires_in = 0) ⇒ Object

This method is called to set a value within this cache store by it’s key.

Parameters:

  • key (String)

    This is the unique key to reference the value being set within this cache store.

  • value (Object)

    This is the value to set within this cache store.

  • expires_in (Integer) (defaults to: 0)

    This is the number of seconds from the current time that this value should expire.



59
60
61
62
63
64
65
66
67
68
# File 'lib/cache_store.rb', line 59

def set(key, value, expires_in = 0)
  remove(build_key(key))
  expires = nil
  if expires_in > 0
    #now = DateTime.now
    #expires = DateTime.new(now.year, now.month, now.day, 0, 0, expires_in)
    expires = Time.now.utc + expires_in
  end
  @store.push({ key: build_key(key), value: value, expires: expires})
end