Sha256: cdced2bf94c5bfc9618e21e3d1f9331e6866cefdbcab1096df49c37fede703e5

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

module Merb
  module Caching
    module Store
      
      class MemoryCache
      
        def initialize(opts={})
          @opts = opts
          @cache = Hash.new
          @timestamps = Hash.new
          @mutex = Mutex.new
          @cache_ttl = @opts.fetch(:session_ttl, 30*60) # default 30 minutes
        end
        
        def [](key)
          @mutex.synchronize {
            @timestamps[key] = Time.now
            @cache[key]
          }
        end
        alias_method :get, :[]
        alias_method :read, :[]
         
        def []=(key, val) 
          @mutex.synchronize {
            @timestamps[key] = Time.now
            @cache[key] = val
          } 
        end
        alias_method :put, :[]=
        alias_method :write, :[]=
        
        def delete(key)
          @mutex.synchronize {
            @cache.delete(key)
          }
        end
        alias_method :remove, :delete
        
        def delete_if(&block)
          @hash.delete_if(&block)
        end
        
        def reap_old_caches
          @timestamps.each do |key,stamp|
            if stamp + @cache_ttl < Time.now
              delete(key)
            end  
          end
          GC.start
        end  
        
        def cache
          @cache
        end  
        
        def keys
          @cache.keys 
        end  
              
      end 
    
    end
  
  end    
  
end  

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
merb-0.3.0 lib/merb/caching/store/memory_cache.rb
merb-0.3.1 lib/merb/caching/store/memory_cache.rb
merb-0.3.3 lib/merb/caching/store/memory_cache.rb
merb-0.2.0 lib/merb/caching/store/memory_cache.rb
merb-0.3.4 lib/merb/caching/store/memory_cache.rb
merb-0.3.7 lib/merb/caching/store/memory_cache.rb