Sha256: d12a91c41f8ae353277cd2620b5301cdcd28e45c24622a6b94a9ee334b0994b3

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cachetastic-1.0.6 lib/stores/cachetastic_stores_local_memory.rb
cachetastic-1.0.7 lib/stores/cachetastic_stores_local_memory.rb
cachetastic-1.0.8 lib/stores/cachetastic_stores_local_memory.rb
cachetastic-1.1.3 lib/stores/cachetastic_stores_local_memory.rb