Sha256: 7ce0e26942620f7f79b30cf148aa03a4761de5b41707e31055ce370b4de75394

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

module Plezi
  module Base
    # Provides a thread-safe caching engine
    module HasCache
      # initializes the cache
      def self.extended(base)
        base.instance_variable_set :@_lock, Mutex.new
        base.instance_variable_set :@_cache, {}.dup
      end

      # Stores data in the cache
      def store(key, value)
        @_lock.synchronize { @_cache[key] = value }
      end
      alias []= store
      # Retrieves data form the cache
      def get(key)
        @_lock.synchronize { @_cache[key] }
      end
      alias [] get
    end
    # Provides thread-specific caching engine, allowing lockless cache at the expenss of memory.
    module HasStore
      # Stores data in the cache
      def store(key, value)
        (Thread.current[(@_chache_name ||= object_id.to_s(16))] ||= {}.dup)[key] = value
      end
      alias []= store
      # Retrieves data form the cache
      def get(key)
        (Thread.current[(@_chache_name ||= object_id.to_s(16))] ||= {}.dup)[key]
      end
      alias [] get
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
plezi-0.14.4 lib/plezi/render/has_cache.rb
plezi-0.14.3 lib/plezi/render/has_cache.rb
plezi-0.14.2 lib/plezi/render/has_cache.rb
plezi-0.14.1 lib/plezi/render/has_cache.rb
plezi-0.14.0 lib/plezi/render/has_cache.rb