Sha256: 95aa3ab2f644242a1bfd4e297aa2c0bb18a18822137d5e78eaf7277be258d1f2

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'glue/cache/memory'

# Global scoped variables. This is backed by a Cache store.

class Global

  # The address of the store.
  
  setting :cache_address, :default => '127.0.0.1', :doc => 'The address of the store'

  # The port of the store.
  
  setting :cache_port, :default => 9079, :doc => 'The port of the store'

  # The cache store that backs global variables.
  
  setting :cache, :default => ::Glue::MemoryCache.new, :doc => 'The cache store that backs global variables'
  
  class << self
  
    # Initialize a global value once.
    
    def init(key, value)
      unless Global[key]
        Global[key] = value
      end
    end
    
    def set(key, value)
      Global.cache[key] = value
    end
    alias_method :[]=, :set
    
    def get(key)
      return Global.cache[key]
    end
    alias_method :[], :get

    # If block is given it acts as an update methods,
    # that transparently handles distributed stores.
    #
    # Global.update(:USERS) do |users|
    #   users << 'gmosx'
    # end
         
    def update(key)
      if block_given?
        # update, also handles distributed stores.
        val = Global.cache[key]
        yield val
        Global.cache[key] = val
      end    
    end
    
    def delete(key)
      Global.cache.delete(key)
    end

  end
  
end

# * George Moschovitis <gm@navel.gr>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.30.0 lib/nitro/global.rb
nitro-0.31.0 lib/nitro/global.rb