Module: Moneta

Defined in:
lib/moneta.rb,
lib/moneta/lock.rb,
lib/moneta/pool.rb,
lib/moneta/weak.rb,
lib/moneta/cache.rb,
lib/moneta/proxy.rb,
lib/moneta/stack.rb,
lib/moneta/utils.rb,
lib/moneta/logger.rb,
lib/moneta/server.rb,
lib/moneta/shared.rb,
lib/moneta/builder.rb,
lib/moneta/expires.rb,
lib/moneta/version.rb,
lib/moneta/wrapper.rb,
lib/moneta/defaults.rb,
lib/moneta/fallback.rb,
lib/moneta/enumerable.rb,
lib/moneta/nil_values.rb,
lib/moneta/dbm_adapter.rb,
lib/moneta/synchronize.rb,
lib/moneta/transformer.rb,
lib/moneta/adapters/dbm.rb,
lib/moneta/adapters/fog.rb,
lib/moneta/adapters/tdb.rb,
lib/moneta/hash_adapter.rb,
lib/moneta/optionmerger.rb,
lib/moneta/adapters/file.rb,
lib/moneta/adapters/gdbm.rb,
lib/moneta/adapters/lmdb.rb,
lib/moneta/adapters/null.rb,
lib/moneta/adapters/riak.rb,
lib/moneta/adapters/sdbm.rb,
lib/moneta/adapters/yaml.rb,
lib/moneta/weak_each_key.rb,
lib/moneta/adapters/couch.rb,
lib/moneta/adapters/hbase.rb,
lib/moneta/adapters/mongo.rb,
lib/moneta/adapters/redis.rb,
lib/moneta/create_support.rb,
lib/moneta/option_support.rb,
lib/moneta/adapters/client.rb,
lib/moneta/adapters/cookie.rb,
lib/moneta/adapters/memory.rb,
lib/moneta/adapters/pstore.rb,
lib/moneta/adapters/sequel.rb,
lib/moneta/adapters/sqlite.rb,
lib/moneta/expires_support.rb,
lib/moneta/adapters/leveldb.rb,
lib/moneta/adapters/lruhash.rb,
lib/moneta/each_key_support.rb,
lib/moneta/adapters/daybreak.rb,
lib/moneta/increment_support.rb,
lib/moneta/adapters/cassandra.rb,
lib/moneta/adapters/memcached.rb,
lib/moneta/transformer/config.rb,
lib/moneta/transformer/helper.rb,
lib/moneta/adapters/datamapper.rb,
lib/moneta/adapters/restclient.rb,
lib/moneta/adapters/tokyotyrant.rb,
lib/moneta/adapters/activerecord.rb,
lib/moneta/adapters/kyotocabinet.rb,
lib/moneta/adapters/sequel/mysql.rb,
lib/moneta/adapters/tokyocabinet.rb,
lib/moneta/adapters/localmemcache.rb,
lib/moneta/adapters/sequel/sqlite.rb,
lib/moneta/transformer/helper/bson.rb,
lib/moneta/adapters/memcached/dalli.rb,
lib/moneta/adapters/sequel/postgres.rb,
lib/moneta/adapters/memcached/native.rb,
lib/moneta/adapters/activesupportcache.rb,
lib/moneta/adapters/sequel/postgres_hstore.rb

Overview

Provides two methods for constructing stores:

Defined Under Namespace

Modules: Adapters, CreateSupport, DBMAdapter, Defaults, EachKeySupport, ExpiresSupport, HashAdapter, IncrementSupport, NilValues, OptionSupport, Utils Classes: Builder, Cache, Enumerable, Expires, Fallback, Lock, Logger, Mutex, OptionMerger, Pool, Proxy, Semaphore, Server, Shared, Stack, SynchronizePrimitive, Transformer, WeakCreate, WeakEachKey, WeakIncrement, Wrapper

Constant Summary collapse

VERSION =

Moneta version number

'1.4.2'.freeze

Class Method Summary collapse

Class Method Details

.build {|Builder| ... } ⇒ Moneta store

Configure your own Moneta proxy stack

Examples:

Moneta builder

Moneta.build do
  use :Expires
  adapter :Memory
end

Yield Parameters:

  • Builder

    block

Returns:

  • (Moneta store)

    newly created Moneta store



164
165
166
# File 'lib/moneta.rb', line 164

def self.build(&block)
  Builder.new(&block).build.last
end

.new(name, options = {}) ⇒ Moneta store

Create new Moneta store with default proxies

This works in most cases if you don’t want fine control over the proxy stack. It uses Marshal on the keys and values. Use Moneta#build if you want to have fine control!

Supported adapters:

  • :HashFile (Store which spreads the entries using a md5 hash, e.g. cache/42/391dd7535aebef91b823286ac67fcd)

  • :File (normal file store)

  • :Memcached (Memcached store)

  • … (All other adapters from Moneta::Adapters)

Parameters:

  • name (Symbol)

    Name of adapter (See Moneta::Adapters)

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Boolean/Integer)

    Ensure that store supports expiration by inserting Expires if the underlying adapter doesn’t support it natively and set default expiration time

  • :threadsafe (Boolean) — default: false

    Ensure that the store is thread safe by inserting Moneta::Lock

  • :logger (Boolean/Hash) — default: false

    Add logger to proxy stack (Hash is passed to logger as options)

  • :compress (Boolean/Symbol) — default: false

    If true, compress value with zlib, or specify custom compress, e.g. :quicklz

  • :serializer (Symbol) — default: :marshal

    Serializer used for key and value, disable with nil

  • :key_serializer (Symbol) — default: options[:serializer]

    Serializer used for key, disable with nil

  • :value_serializer (Symbol) — default: options[:serializer]

    Serializer used for value, disable with nil

  • :prefix (String)

    Key prefix used for namespacing (default none)

  • All (Object)

    other options passed to the adapter

Returns:

  • (Moneta store)

    newly created Moneta store

Raises:

  • (ArgumentError)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/moneta.rb', line 105

def self.new(name, options = {})
  expires = options[:expires]
  options.delete(:expires) unless Numeric === expires
  logger = options.delete(:logger)
  threadsafe = options.delete(:threadsafe)
  compress = options.delete(:compress)
  serializer = options.include?(:serializer) ? options.delete(:serializer) : :marshal
  key_serializer = options.include?(:key_serializer) ? options.delete(:key_serializer) : serializer
  value_serializer = options.include?(:value_serializer) ? options.delete(:value_serializer) : serializer
  transformer = { key: [key_serializer, :prefix], value: [value_serializer], prefix: options.delete(:prefix) }
  transformer[:value] << (Symbol === compress ? compress : :zlib) if compress
  raise ArgumentError, 'Name must be Symbol' unless Symbol === name
  case name
  when :Sequel
    # Sequel accept only base64 keys
    transformer[:key] << :base64
    # If using HStore, binary data is not allowed
    transformer[:value] << :base64 if options[:hstore]
  when :ActiveRecord, :DataMapper
    # DataMapper and AR accept only base64 keys and values
    transformer[:key] << :base64
    transformer[:value] << :base64
  when :Couch
    # CouchDB needs to use URL-safe Base64 for its keys
    transformer[:key] << :urlsafe_base64
    transformer[:value] << :base64
  when :PStore, :YAML, :Null
    # For PStore and YAML only the key has to be a string
    transformer.delete(:value) if transformer[:value] == [:marshal]
  when :HashFile
    # Use spreading hashes
    transformer[:key] << :md5 << :spread
    name = :File
  when :File, :Riak, :RestClient
    # Use escaping for file and HTTP interfaces
    transformer[:key] << :escape
  end
  a = Adapters.const_get(name).new(options)
  build do
    use :Logger, Hash === logger ? logger : {} if logger
    use :Expires, expires: options[:expires] if !a.supports?(:expires) && expires
    use :Transformer, transformer
    use :Lock if threadsafe
    adapter a
  end
end