class Cachetastic::Stores::LocalMemory < Cachetastic::Stores::Base attr_accessor :local_store def valid? true end def setup self.local_store = {} end def expire_all self.local_store = {} end def get(key) so = self.local_store[key.to_s] if so if Time.now >= so.expires_at 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] = 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 private class StoreObject attr_accessor :key attr_accessor :value attr_accessor :expires_at def initialize(key, value, expiry) self.key = key self.value = value begin self.expires_at = (Time.now + (expiry == 0 ? (31536000) : expiry)) # 31536000 = one year rescue RangeError => e self.expires_at = Time.at(expiry) end # puts "now: #{Time.now}" # puts "expiry: #{expiry}" # puts "expires_at: #{self.expires_at}" end def size return self.value.size if self.value.respond_to?(:size) -1 end end end