moneta 0.7.20 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -5
- data/CHANGES +6 -0
- data/CONTRIBUTORS +2 -0
- data/Gemfile +31 -44
- data/LICENSE +1 -1
- data/README.md +82 -59
- data/lib/active_support/cache/moneta_store.rb +3 -3
- data/lib/moneta.rb +4 -2
- data/lib/moneta/adapters/activerecord.rb +8 -8
- data/lib/moneta/adapters/cassandra.rb +8 -8
- data/lib/moneta/adapters/couch.rb +1 -1
- data/lib/moneta/adapters/datamapper.rb +5 -5
- data/lib/moneta/adapters/daybreak.rb +1 -1
- data/lib/moneta/adapters/fog.rb +2 -2
- data/lib/moneta/adapters/lmdb.rb +1 -1
- data/lib/moneta/adapters/localmemcache.rb +1 -1
- data/lib/moneta/adapters/memcached/dalli.rb +3 -3
- data/lib/moneta/adapters/memcached/native.rb +1 -1
- data/lib/moneta/adapters/mongo.rb +6 -164
- data/lib/moneta/adapters/mongo/base.rb +63 -0
- data/lib/moneta/adapters/mongo/moped.rb +113 -0
- data/lib/moneta/adapters/mongo/official.rb +121 -0
- data/lib/moneta/adapters/pstore.rb +1 -5
- data/lib/moneta/adapters/restclient.rb +1 -1
- data/lib/moneta/adapters/sequel.rb +10 -10
- data/lib/moneta/cache.rb +1 -1
- data/lib/moneta/logger.rb +2 -2
- data/lib/moneta/mixins.rb +4 -4
- data/lib/moneta/shared.rb +2 -2
- data/lib/moneta/stack.rb +2 -2
- data/lib/moneta/synchronize.rb +2 -2
- data/lib/moneta/transformer.rb +5 -5
- data/lib/moneta/transformer/config.rb +38 -40
- data/lib/moneta/version.rb +1 -1
- data/lib/rack/cache/moneta.rb +1 -1
- data/lib/rack/moneta_cookies.rb +2 -2
- data/lib/rack/moneta_rest.rb +1 -1
- data/lib/rack/moneta_store.rb +4 -4
- data/lib/rack/session/moneta.rb +14 -2
- data/moneta.gemspec +1 -0
- data/script/benchmarks +97 -96
- data/script/generate-specs +763 -730
- data/script/install-bundle +1 -1
- data/script/install-kyotocabinet +2 -2
- data/script/memusage +1 -1
- data/script/upload-bundle +1 -1
- data/spec/action_dispatch/session_moneta_store_spec.rb +5 -5
- data/spec/active_support/cache_moneta_store_spec.rb +20 -20
- data/spec/moneta/adapter_activerecord_exisiting_connection_spec.rb +2 -2
- data/spec/moneta/adapter_activerecord_spec.rb +6 -6
- data/spec/moneta/adapter_cassandra_spec.rb +1 -1
- data/spec/moneta/adapter_cassandra_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapter_couch_spec.rb +1 -1
- data/spec/moneta/adapter_datamapper_spec.rb +6 -6
- data/spec/moneta/adapter_daybreak_spec.rb +1 -1
- data/spec/moneta/adapter_dbm_spec.rb +1 -1
- data/spec/moneta/adapter_file_spec.rb +1 -1
- data/spec/moneta/adapter_fog_spec.rb +4 -4
- data/spec/moneta/adapter_gdbm_spec.rb +1 -1
- data/spec/moneta/adapter_hbase_spec.rb +1 -1
- data/spec/moneta/adapter_kyotocabinet_spec.rb +1 -1
- data/spec/moneta/adapter_leveldb_spec.rb +1 -1
- data/spec/moneta/adapter_lmdb_spec.rb +1 -1
- data/spec/moneta/adapter_lmdb_with_db_spec.rb +1 -1
- data/spec/moneta/adapter_localmemcache_spec.rb +1 -1
- data/spec/moneta/adapter_lruhash_spec.rb +1 -1
- data/spec/moneta/adapter_memcached_dalli_spec.rb +1 -1
- data/spec/moneta/adapter_memcached_dalli_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapter_memcached_native_spec.rb +1 -1
- data/spec/moneta/adapter_memcached_native_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapter_memcached_spec.rb +1 -1
- data/spec/moneta/adapter_memcached_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapter_mongo_moped_spec.rb +56 -0
- data/spec/moneta/adapter_mongo_moped_with_default_expires_spec.rb +51 -0
- data/spec/moneta/adapter_mongo_official_spec.rb +56 -0
- data/spec/moneta/adapter_mongo_official_with_default_expires_spec.rb +51 -0
- data/spec/moneta/adapter_mongo_spec.rb +1 -7
- data/spec/moneta/adapter_mongo_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapter_pstore_spec.rb +1 -1
- data/spec/moneta/adapter_redis_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapter_restclient_spec.rb +1 -1
- data/spec/moneta/adapter_sdbm_spec.rb +1 -1
- data/spec/moneta/adapter_sequel_spec.rb +1 -1
- data/spec/moneta/adapter_sqlite_spec.rb +1 -1
- data/spec/moneta/adapter_tdb_spec.rb +1 -1
- data/spec/moneta/adapter_tokyocabinet_bdb_spec.rb +1 -1
- data/spec/moneta/adapter_tokyocabinet_hdb_spec.rb +1 -1
- data/spec/moneta/adapter_yaml_spec.rb +1 -1
- data/spec/moneta/cache_file_memory_spec.rb +1 -1
- data/spec/moneta/expires_file_spec.rb +3 -3
- data/spec/moneta/expires_memory_with_default_expires_spec.rb +1 -1
- data/spec/moneta/mutex_spec.rb +1 -1
- data/spec/moneta/optionmerger_spec.rb +26 -26
- data/spec/moneta/pool_spec.rb +1 -1
- data/spec/moneta/semaphore_spec.rb +1 -1
- data/spec/moneta/shared_tcp_spec.rb +2 -2
- data/spec/moneta/shared_unix_spec.rb +2 -2
- data/spec/moneta/stack_file_memory_spec.rb +1 -1
- data/spec/moneta/stack_memory_file_spec.rb +1 -1
- data/spec/moneta/standard_activerecord_spec.rb +1 -1
- data/spec/moneta/standard_activerecord_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_cassandra_spec.rb +1 -1
- data/spec/moneta/standard_client_tcp_spec.rb +1 -1
- data/spec/moneta/standard_client_unix_spec.rb +2 -2
- data/spec/moneta/standard_couch_spec.rb +1 -1
- data/spec/moneta/standard_couch_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_datamapper_spec.rb +2 -2
- data/spec/moneta/standard_datamapper_with_expires_spec.rb +2 -2
- data/spec/moneta/standard_datamapper_with_repository_spec.rb +2 -2
- data/spec/moneta/standard_daybreak_spec.rb +1 -1
- data/spec/moneta/standard_daybreak_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_dbm_spec.rb +1 -1
- data/spec/moneta/standard_dbm_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_file_spec.rb +1 -1
- data/spec/moneta/standard_file_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_fog_spec.rb +4 -4
- data/spec/moneta/standard_fog_with_expires_spec.rb +5 -5
- data/spec/moneta/standard_gdbm_spec.rb +1 -1
- data/spec/moneta/standard_gdbm_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_hashfile_spec.rb +1 -1
- data/spec/moneta/standard_hashfile_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_hbase_spec.rb +1 -1
- data/spec/moneta/standard_hbase_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_kyotocabinet_spec.rb +1 -1
- data/spec/moneta/standard_kyotocabinet_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_leveldb_spec.rb +1 -1
- data/spec/moneta/standard_leveldb_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_lmdb_spec.rb +1 -1
- data/spec/moneta/standard_lmdb_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_localmemcache_spec.rb +1 -1
- data/spec/moneta/standard_localmemcache_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_lruhash_spec.rb +1 -1
- data/spec/moneta/standard_lruhash_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_memcached_dalli_spec.rb +1 -1
- data/spec/moneta/standard_memcached_native_spec.rb +1 -1
- data/spec/moneta/standard_memcached_spec.rb +1 -1
- data/spec/moneta/standard_memory_spec.rb +1 -1
- data/spec/moneta/standard_memory_with_compress_spec.rb +1 -1
- data/spec/moneta/standard_memory_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_memory_with_json_key_serializer_spec.rb +1 -1
- data/spec/moneta/standard_memory_with_json_serializer_spec.rb +1 -1
- data/spec/moneta/standard_memory_with_json_value_serializer_spec.rb +1 -1
- data/spec/moneta/standard_memory_with_prefix_spec.rb +1 -1
- data/spec/moneta/standard_memory_with_snappy_compress_spec.rb +1 -1
- data/spec/moneta/standard_mongo_moped_spec.rb +255 -0
- data/spec/moneta/standard_mongo_official_spec.rb +255 -0
- data/spec/moneta/standard_mongo_spec.rb +1 -1
- data/spec/moneta/standard_null_spec.rb +1 -1
- data/spec/moneta/standard_pstore_spec.rb +1 -1
- data/spec/moneta/standard_pstore_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_redis_spec.rb +1 -1
- data/spec/moneta/standard_restclient_spec.rb +1 -1
- data/spec/moneta/standard_riak_spec.rb +1 -1
- data/spec/moneta/standard_riak_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_sdbm_spec.rb +1 -1
- data/spec/moneta/standard_sdbm_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_sequel_spec.rb +1 -1
- data/spec/moneta/standard_sequel_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_sqlite_spec.rb +1 -1
- data/spec/moneta/standard_sqlite_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_tdb_spec.rb +1 -1
- data/spec/moneta/standard_tdb_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_tokyocabinet_spec.rb +1 -1
- data/spec/moneta/standard_tokyocabinet_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_tokyotyrant_spec.rb +1 -1
- data/spec/moneta/standard_tokyotyrant_with_expires_spec.rb +1 -1
- data/spec/moneta/standard_yaml_spec.rb +1 -1
- data/spec/moneta/standard_yaml_with_expires_spec.rb +1 -1
- data/spec/moneta/transformer_bencode_spec.rb +1 -1
- data/spec/moneta/transformer_bert_spec.rb +1 -1
- data/spec/moneta/transformer_bson_spec.rb +1 -1
- data/spec/moneta/transformer_bzip2_spec.rb +1 -1
- data/spec/moneta/transformer_json_spec.rb +1 -1
- data/spec/moneta/transformer_key_inspect_spec.rb +1 -1
- data/spec/moneta/transformer_key_marshal_spec.rb +1 -1
- data/spec/moneta/transformer_key_to_s_spec.rb +1 -1
- data/spec/moneta/transformer_key_yaml_spec.rb +1 -1
- data/spec/moneta/transformer_lz4_spec.rb +1 -1
- data/spec/moneta/transformer_lzma_spec.rb +1 -1
- data/spec/moneta/transformer_lzo_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_base64_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_city128_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_city32_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_city64_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_escape_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_hex_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_hmac_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_md5_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_md5_spread_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_prefix_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_qp_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_rmd160_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_sha1_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_sha256_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_sha384_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_sha512_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_truncate_spec.rb +1 -1
- data/spec/moneta/transformer_marshal_uuencode_spec.rb +1 -1
- data/spec/moneta/transformer_msgpack_spec.rb +1 -1
- data/spec/moneta/transformer_ox_spec.rb +1 -1
- data/spec/moneta/transformer_php_spec.rb +1 -1
- data/spec/moneta/transformer_quicklz_spec.rb +1 -1
- data/spec/moneta/transformer_snappy_spec.rb +1 -1
- data/spec/moneta/transformer_tnet_spec.rb +1 -1
- data/spec/moneta/transformer_value_marshal_spec.rb +1 -1
- data/spec/moneta/transformer_value_yaml_spec.rb +1 -1
- data/spec/moneta/transformer_yaml_spec.rb +1 -1
- data/spec/moneta/transformer_zlib_spec.rb +1 -1
- data/spec/moneta/weak_create_spec.rb +4 -4
- data/spec/moneta/weak_increment_spec.rb +4 -4
- data/spec/monetaspecs.rb +481 -481
- metadata +14 -185
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'moneta/adapters/mongo/base'
|
2
|
+
require 'moped'
|
3
|
+
|
4
|
+
module Moneta
|
5
|
+
module Adapters
|
6
|
+
# MongoDB backend
|
7
|
+
#
|
8
|
+
# Supports expiration, documents will be automatically removed starting
|
9
|
+
# with mongodb >= 2.2 (see {http://docs.mongodb.org/manual/tutorial/expire-data/}).
|
10
|
+
#
|
11
|
+
# You can store hashes directly using this adapter.
|
12
|
+
#
|
13
|
+
# @example Store hashes
|
14
|
+
# db = Moneta::Adapters::MongoMoped.new
|
15
|
+
# db['key'] = {a: 1, b: 2}
|
16
|
+
#
|
17
|
+
# @api public
|
18
|
+
class MongoMoped < MongoBase
|
19
|
+
# @param [Hash] options
|
20
|
+
# @option options [String] :collection ('moneta') MongoDB collection name
|
21
|
+
# @option options [String] :host ('127.0.0.1') MongoDB server host
|
22
|
+
# @option options [String] :user Username used to authenticate
|
23
|
+
# @option options [String] :password Password used to authenticate
|
24
|
+
# @option options [Integer] :port (MongoDB default port) MongoDB server port
|
25
|
+
# @option options [String] :db ('moneta') MongoDB database
|
26
|
+
# @option options [Integer] :expires Default expiration time
|
27
|
+
# @option options [String] :expires_field ('expiresAt') Document field to store expiration time
|
28
|
+
# @option options [String] :value_field ('value') Document field to store value
|
29
|
+
# @option options [String] :type_field ('type') Document field to store value type
|
30
|
+
# @option options [::Moped::Session] :backend Use existing backend instance
|
31
|
+
# @option options Other options passed to `Moped::Session#new`
|
32
|
+
def initialize(options = {})
|
33
|
+
super(options)
|
34
|
+
collection = options.delete(:collection) || 'moneta'
|
35
|
+
db = options.delete(:db) || 'moneta'
|
36
|
+
user = options.delete(:user)
|
37
|
+
password = options.delete(:password)
|
38
|
+
@backend = options[:backend] ||
|
39
|
+
begin
|
40
|
+
host = options.delete(:host) || '127.0.0.1'
|
41
|
+
port = options.delete(:port) || 27017
|
42
|
+
::Moped::Session.new(["#{host}:#{port}"])
|
43
|
+
end
|
44
|
+
@backend.use(db)
|
45
|
+
@backend.login(user, password) if user && password
|
46
|
+
@collection = @backend[collection]
|
47
|
+
if @backend.command(buildinfo: 1)['version'] >= '2.2'
|
48
|
+
@collection.indexes.create({ @expires_field => 1 }, expireAfterSeconds: 0)
|
49
|
+
else
|
50
|
+
warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# (see Proxy#load)
|
55
|
+
def load(key, options = {})
|
56
|
+
key = to_binary(key)
|
57
|
+
doc = @collection.find(_id: key).one
|
58
|
+
if doc && (!doc[@expires_field] || doc[@expires_field] >= Time.now)
|
59
|
+
# @expires_field must be a Time object (BSON date datatype)
|
60
|
+
expires = expires_at(options, nil)
|
61
|
+
@collection.find(_id: key).update(:$set => { @expires_field => expires || nil }) if expires != nil
|
62
|
+
doc_to_value(doc)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# (see Proxy#store)
|
67
|
+
def store(key, value, options = {})
|
68
|
+
key = to_binary(key)
|
69
|
+
@collection.find(_id: key).upsert(value_to_doc(key, value, options))
|
70
|
+
value
|
71
|
+
end
|
72
|
+
|
73
|
+
# (see Proxy#delete)
|
74
|
+
def delete(key, options = {})
|
75
|
+
value = load(key, options)
|
76
|
+
@collection.find(_id: to_binary(key)).remove if value
|
77
|
+
value
|
78
|
+
end
|
79
|
+
|
80
|
+
# (see Proxy#increment)
|
81
|
+
def increment(key, amount = 1, options = {})
|
82
|
+
@backend.with(safe: true, consistency: :strong) do |safe|
|
83
|
+
safe[@collection.name].find(_id: to_binary(key)).modify({:$inc => { @value_field => amount }},
|
84
|
+
new: true, upsert: true)[@value_field]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# (see Proxy#create)
|
89
|
+
def create(key, value, options = {})
|
90
|
+
key = to_binary(key)
|
91
|
+
@backend.with(safe: true, consistency: :strong) do |safe|
|
92
|
+
safe[@collection.name].insert(value_to_doc(key, value, options))
|
93
|
+
end
|
94
|
+
true
|
95
|
+
rescue ::Moped::Errors::MongoError => ex
|
96
|
+
raise if ex.details['code'] != 11000 # duplicate key error
|
97
|
+
false
|
98
|
+
end
|
99
|
+
|
100
|
+
# (see Proxy#clear)
|
101
|
+
def clear(options = {})
|
102
|
+
@collection.drop
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
protected
|
107
|
+
|
108
|
+
def to_binary(s)
|
109
|
+
::Moped::BSON::Binary.new(:generic, s)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'moneta/adapters/mongo/base'
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
module Moneta
|
5
|
+
module Adapters
|
6
|
+
# MongoDB backend
|
7
|
+
#
|
8
|
+
# Supports expiration, documents will be automatically removed starting
|
9
|
+
# with mongodb >= 2.2 (see {http://docs.mongodb.org/manual/tutorial/expire-data/}).
|
10
|
+
#
|
11
|
+
# You can store hashes directly using this adapter.
|
12
|
+
#
|
13
|
+
# @example Store hashes
|
14
|
+
# db = Moneta::Adapters::MongoOfficial.new
|
15
|
+
# db['key'] = {a: 1, b: 2}
|
16
|
+
#
|
17
|
+
# @api public
|
18
|
+
class MongoOfficial < MongoBase
|
19
|
+
# @param [Hash] options
|
20
|
+
# @option options [String] :collection ('moneta') MongoDB collection name
|
21
|
+
# @option options [String] :host ('127.0.0.1') MongoDB server host
|
22
|
+
# @option options [String] :user Username used to authenticate
|
23
|
+
# @option options [String] :password Password used to authenticate
|
24
|
+
# @option options [Integer] :port (MongoDB default port) MongoDB server port
|
25
|
+
# @option options [String] :db ('moneta') MongoDB database
|
26
|
+
# @option options [Integer] :expires Default expiration time
|
27
|
+
# @option options [String] :expires_field ('expiresAt') Document field to store expiration time
|
28
|
+
# @option options [String] :value_field ('value') Document field to store value
|
29
|
+
# @option options [String] :type_field ('type') Document field to store value type
|
30
|
+
# @option options [::Mongo::MongoClient] :backend Use existing backend instance
|
31
|
+
# @option options Other options passed to `Mongo::MongoClient#new`
|
32
|
+
def initialize(options = {})
|
33
|
+
super(options)
|
34
|
+
collection = options.delete(:collection) || 'moneta'
|
35
|
+
db = options.delete(:db) || 'moneta'
|
36
|
+
user = options.delete(:user)
|
37
|
+
password = options.delete(:password)
|
38
|
+
@backend = options[:backend] ||
|
39
|
+
begin
|
40
|
+
host = options.delete(:host) || '127.0.0.1'
|
41
|
+
port = options.delete(:port) || ::Mongo::MongoClient::DEFAULT_PORT
|
42
|
+
::Mongo::MongoClient.new(host, port, options)
|
43
|
+
end
|
44
|
+
db = @backend.db(db)
|
45
|
+
db.authenticate(user, password, true) if user && password
|
46
|
+
@collection = db.collection(collection)
|
47
|
+
if @backend.server_version >= '2.2'
|
48
|
+
@collection.ensure_index([[@expires_field, ::Mongo::ASCENDING]], expireAfterSeconds: 0)
|
49
|
+
else
|
50
|
+
warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# (see Proxy#load)
|
55
|
+
def load(key, options = {})
|
56
|
+
key = to_binary(key)
|
57
|
+
doc = @collection.find_one('_id' => key)
|
58
|
+
if doc && (!doc[@expires_field] || doc[@expires_field] >= Time.now)
|
59
|
+
expires = expires_at(options, nil)
|
60
|
+
@collection.update({ '_id' => key },
|
61
|
+
# @expires_field must be a Time object (BSON date datatype)
|
62
|
+
{ '$set' => { @expires_field => expires || nil } }) if expires != nil
|
63
|
+
doc_to_value(doc)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# (see Proxy#store)
|
68
|
+
def store(key, value, options = {})
|
69
|
+
key = to_binary(key)
|
70
|
+
@collection.update({ '_id' => key },
|
71
|
+
value_to_doc(key, value, options),
|
72
|
+
{ upsert: true })
|
73
|
+
value
|
74
|
+
end
|
75
|
+
|
76
|
+
# (see Proxy#delete)
|
77
|
+
def delete(key, options = {})
|
78
|
+
value = load(key, options)
|
79
|
+
@collection.remove('_id' => to_binary(key)) if value
|
80
|
+
value
|
81
|
+
end
|
82
|
+
|
83
|
+
# (see Proxy#increment)
|
84
|
+
def increment(key, amount = 1, options = {})
|
85
|
+
@collection.find_and_modify(query: { '_id' => to_binary(key) },
|
86
|
+
update: { '$inc' => { @value_field => amount } },
|
87
|
+
new: true,
|
88
|
+
upsert: true)[@value_field]
|
89
|
+
end
|
90
|
+
|
91
|
+
# (see Proxy#create)
|
92
|
+
def create(key, value, options = {})
|
93
|
+
key = to_binary(key)
|
94
|
+
@collection.insert(value_to_doc(key, value, options))
|
95
|
+
true
|
96
|
+
rescue ::Mongo::OperationFailure => ex
|
97
|
+
raise if ex.error_code != 11000 # duplicate key error
|
98
|
+
false
|
99
|
+
end
|
100
|
+
|
101
|
+
# (see Proxy#clear)
|
102
|
+
def clear(options = {})
|
103
|
+
@collection.remove
|
104
|
+
self
|
105
|
+
end
|
106
|
+
|
107
|
+
# (see Proxy#close)
|
108
|
+
def close
|
109
|
+
@backend.close
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
|
113
|
+
protected
|
114
|
+
|
115
|
+
def to_binary(s)
|
116
|
+
s = s.dup if s.frozen? # HACK: BSON::Binary needs unfrozen string
|
117
|
+
::BSON::Binary.new(s)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -77,11 +77,7 @@ module Moneta
|
|
77
77
|
protected
|
78
78
|
|
79
79
|
def new_store(options)
|
80
|
-
|
81
|
-
::PStore.new(options[:file], options[:threadsafe])
|
82
|
-
else
|
83
|
-
::PStore.new(options[:file])
|
84
|
-
end
|
80
|
+
::PStore.new(options[:file], options[:threadsafe])
|
85
81
|
end
|
86
82
|
end
|
87
83
|
end
|
@@ -14,7 +14,7 @@ module Moneta
|
|
14
14
|
# @option options [Faraday connection] :backend Use existing backend instance
|
15
15
|
def initialize(options = {})
|
16
16
|
raise ArgumentError, 'Option :url is required' unless url = options[:url]
|
17
|
-
@backend = options[:backend] || ::Faraday.new(:
|
17
|
+
@backend = options[:backend] || ::Faraday.new(url: url)
|
18
18
|
end
|
19
19
|
|
20
20
|
# (see Proxy#key?)
|
@@ -27,7 +27,7 @@ module Moneta
|
|
27
27
|
::Sequel.connect(db, options)
|
28
28
|
end
|
29
29
|
@backend.create_table?(table) do
|
30
|
-
String :k, :
|
30
|
+
String :k, null: false, primary_key: true
|
31
31
|
Blob :v
|
32
32
|
end
|
33
33
|
@table = @backend[table]
|
@@ -35,21 +35,21 @@ module Moneta
|
|
35
35
|
|
36
36
|
# (see Proxy#key?)
|
37
37
|
def key?(key, options = {})
|
38
|
-
@table[:
|
38
|
+
@table[k: key] != nil
|
39
39
|
end
|
40
40
|
|
41
41
|
# (see Proxy#load)
|
42
42
|
def load(key, options = {})
|
43
|
-
record = @table[:
|
43
|
+
record = @table[k: key]
|
44
44
|
record && record[:v]
|
45
45
|
end
|
46
46
|
|
47
47
|
# (see Proxy#store)
|
48
48
|
def store(key, value, options = {})
|
49
49
|
begin
|
50
|
-
@table.insert(:
|
50
|
+
@table.insert(k: key, v: blob(value))
|
51
51
|
rescue UniqueConstraintViolation
|
52
|
-
@table.where(:
|
52
|
+
@table.where(k: key).update(v: blob(value))
|
53
53
|
end
|
54
54
|
value
|
55
55
|
rescue ::Sequel::DatabaseError
|
@@ -59,7 +59,7 @@ module Moneta
|
|
59
59
|
|
60
60
|
# (see Proxy#store)
|
61
61
|
def create(key, value, options = {})
|
62
|
-
@table.insert(:
|
62
|
+
@table.insert(k: key, v: blob(value))
|
63
63
|
true
|
64
64
|
rescue UniqueConstraintViolation
|
65
65
|
false
|
@@ -69,12 +69,12 @@ module Moneta
|
|
69
69
|
def increment(key, amount = 1, options = {})
|
70
70
|
@backend.transaction do
|
71
71
|
locked_table = @table.for_update
|
72
|
-
if record = locked_table[:
|
72
|
+
if record = locked_table[k: key]
|
73
73
|
value = Utils.to_int(record[:v]) + amount
|
74
|
-
locked_table.where(:
|
74
|
+
locked_table.where(k: key).update(v: blob(value.to_s))
|
75
75
|
value
|
76
76
|
else
|
77
|
-
locked_table.insert(:
|
77
|
+
locked_table.insert(k: key, v: blob(amount.to_s))
|
78
78
|
amount
|
79
79
|
end
|
80
80
|
end
|
@@ -87,7 +87,7 @@ module Moneta
|
|
87
87
|
# (see Proxy#delete)
|
88
88
|
def delete(key, options = {})
|
89
89
|
if value = load(key, options)
|
90
|
-
@table.filter(:
|
90
|
+
@table.filter(k: key).delete
|
91
91
|
value
|
92
92
|
end
|
93
93
|
end
|
data/lib/moneta/cache.rb
CHANGED
data/lib/moneta/logger.rb
CHANGED
@@ -64,10 +64,10 @@ module Moneta
|
|
64
64
|
|
65
65
|
def wrap(method, *args)
|
66
66
|
ret = yield
|
67
|
-
@logger.log(:
|
67
|
+
@logger.log(method: method, args: args, return: (method == :clear ? 'self' : ret))
|
68
68
|
ret
|
69
69
|
rescue Exception => error
|
70
|
-
@logger.log(:
|
70
|
+
@logger.log(method: method, args: args, error: error)
|
71
71
|
raise
|
72
72
|
end
|
73
73
|
end
|
data/lib/moneta/mixins.rb
CHANGED
@@ -17,14 +17,14 @@ module Moneta
|
|
17
17
|
options ? OptionMerger.new(adapter, options) : adapter
|
18
18
|
end
|
19
19
|
|
20
|
-
# Return Moneta store with default option :
|
20
|
+
# Return Moneta store with default option raw: true
|
21
21
|
#
|
22
22
|
# @return [OptionMerger]
|
23
23
|
# @api public
|
24
24
|
def raw
|
25
25
|
@raw_store ||=
|
26
26
|
begin
|
27
|
-
store = with(:
|
27
|
+
store = with(raw: true, only: [:load, :store, :create, :delete])
|
28
28
|
store.instance_variable_set(:@raw_store, store)
|
29
29
|
store
|
30
30
|
end
|
@@ -36,7 +36,7 @@ module Moneta
|
|
36
36
|
# @return [OptionMerger]
|
37
37
|
# @api public
|
38
38
|
def prefix(prefix)
|
39
|
-
with(:
|
39
|
+
with(prefix: prefix, except: :clear)
|
40
40
|
end
|
41
41
|
|
42
42
|
# Return Moneta store with default expiration time
|
@@ -45,7 +45,7 @@ module Moneta
|
|
45
45
|
# @return [OptionMerger]
|
46
46
|
# @api public
|
47
47
|
def expires(expires)
|
48
|
-
with(:
|
48
|
+
with(expires: expires, only: [:store, :create, :increment])
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
data/lib/moneta/shared.rb
CHANGED
@@ -3,9 +3,9 @@ module Moneta
|
|
3
3
|
#
|
4
4
|
# @example Share a store
|
5
5
|
# Moneta.build do
|
6
|
-
# use :Transformer, :
|
6
|
+
# use :Transformer, key: :marshal, value: :marshal
|
7
7
|
# use :Shared do
|
8
|
-
# adapter :GDBM, :
|
8
|
+
# adapter :GDBM, file: 'shared.db'
|
9
9
|
# end
|
10
10
|
# end
|
11
11
|
#
|
data/lib/moneta/stack.rb
CHANGED
@@ -6,8 +6,8 @@ module Moneta
|
|
6
6
|
# Moneta.build do
|
7
7
|
# use(:Stack) do
|
8
8
|
# add { adapter :Redis }
|
9
|
-
# add { adapter :File, :
|
10
|
-
# add { adapter :File, :
|
9
|
+
# add { adapter :File, dir: 'data' }
|
10
|
+
# add { adapter :File, dir: 'replicate' }
|
11
11
|
# end
|
12
12
|
# end
|
13
13
|
#
|
data/lib/moneta/synchronize.rb
CHANGED
@@ -80,7 +80,7 @@ module Moneta
|
|
80
80
|
protected
|
81
81
|
|
82
82
|
def enter_primitive
|
83
|
-
@store.create(@lock, '', :
|
83
|
+
@store.create(@lock, '', expires: false)
|
84
84
|
end
|
85
85
|
|
86
86
|
def leave_primitive
|
@@ -105,7 +105,7 @@ module Moneta
|
|
105
105
|
def initialize(store, counter, max = 1)
|
106
106
|
raise 'Store must support feature :increment' unless store.supports?(:increment)
|
107
107
|
@store, @counter, @max = store, counter, max
|
108
|
-
@store.increment(@counter, 0, :
|
108
|
+
@store.increment(@counter, 0, expires: false) # Ensure that counter exists
|
109
109
|
end
|
110
110
|
|
111
111
|
protected
|
data/lib/moneta/transformer.rb
CHANGED
@@ -4,17 +4,17 @@ module Moneta
|
|
4
4
|
#
|
5
5
|
# @example Add `Moneta::Transformer` to proxy stack
|
6
6
|
# Moneta.build do
|
7
|
-
# transformer :
|
8
|
-
# adapter :File, :
|
7
|
+
# transformer key: [:marshal, :escape], value: [:marshal]
|
8
|
+
# adapter :File, dir: 'data'
|
9
9
|
# end
|
10
10
|
#
|
11
11
|
# @example Bypass serialization
|
12
|
-
# store.store('key', 'value', :
|
12
|
+
# store.store('key', 'value', raw: true)
|
13
13
|
# store['key'] # raises an Exception
|
14
|
-
# store.load('key', :
|
14
|
+
# store.load('key', raw: true) # returns 'value'
|
15
15
|
#
|
16
16
|
# store['key'] = 'value'
|
17
|
-
# store.load('key', :
|
17
|
+
# store.load('key', raw: true) # returns "\x04\bI\"\nvalue\x06:\x06ET"
|
18
18
|
#
|
19
19
|
# @api public
|
20
20
|
class Transformer < Proxy
|