Sha256: e64905fd1ad39efb7a2e8283ae8b089dcd1b24ad41ef4cdc2b4b0f4ec564a442

Contents?: true

Size: 1.49 KB

Versions: 18

Compression:

Stored size: 1.49 KB

Contents

# An adapter/store that keeps objects in local memory. This is great for development/testing,
# but probably shouldn't be put into production. 
# It's also a very good example of how to write a adapter.
class Cachetastic::Adapters::LocalMemory < Cachetastic::Adapters::Base
  
  attr_accessor :local_store
  
  def valid?
    true
  end

  def setup
    self.local_store = {}
  end
  
  def expire_all
    self.local_store = {}
  end
  
  # See Cachetastic::Adapters::Base
  def get(key)
    so = self.local_store[key.to_s]
    if so
      if so.invalid?
        self.delete(key)
        return nil
      end
      return so.value
    end
    return nil
  end
  
  def set(key, value, expiry = 0)
    self.local_store[key.to_s] = Cachetastic::Adapters::StoreObject.new(key.to_s, value, expiry)
  end
  
  def delete(key, delay = 0)
    if delay <= 0
      self.local_store.delete(key.to_s)
    else
      so = self.get(key)
      if so
        self.set(so.key, so.value, delay)
      end
    end
  end
  
  def stats
    super
    num_keys = self.local_store.size
    s = "Number of Entries: #{num_keys}\n"
    if num_keys > 0
      expiries = []
      keys = []
      self.local_store.each do |key,value|
        keys << key
        expiries << value.expires_at
      end
      expiries.sort! {|x, y| x <=> y}
      oldest_expiry = expiries.first
      newest_expiry = expiries.last
      s += "Oldest Entry: #{oldest_expiry}\nNewest Entry: #{newest_expiry}\nKeys: #{keys.inspect}\n"
    end
    puts s + "\n"
  end

  
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
cachetastic-1.7.2 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.4.1 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.5.0 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.4.0 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.4.2 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.6.0 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.7.0 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.7.4 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-1.7.3 lib/adapters/cachetastic_adapters_local_memory.rb
cachetastic-2.1.2 lib/cachetastic/adapters/local_memory.rb
cachetastic-2.0.0 lib/cachetastic/adapters/local_memory.rb
cachetastic-2.1.0 lib/cachetastic/adapters/local_memory.rb
cachetastic-2.1.3 lib/cachetastic/adapters/local_memory.rb
cachetastic-2.1.4 lib/cachetastic/adapters/local_memory.rb
cachetastic-2.1.1 lib/cachetastic/adapters/local_memory.rb
mack-caching-0.8.3 lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb
mack-caching-0.8.2 lib/gems/cachetastic-2.0.0/lib/cachetastic/adapters/local_memory.rb
mack-caching-0.8.3.1 lib/gems/cachetastic-2.1.2/lib/cachetastic/adapters/local_memory.rb