moneta 1.0.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +425 -0
- data/.gitignore +3 -0
- data/.rspec +4 -0
- data/.rubocop.yml +194 -0
- data/.yardopts +6 -1
- data/CHANGES +88 -0
- data/CONTRIBUTORS +10 -2
- data/Gemfile +198 -76
- data/LICENSE +1 -1
- data/README.md +181 -83
- data/SPEC.md +70 -0
- data/feature_matrix.yaml +227 -0
- data/lib/action_dispatch/middleware/session/moneta_store.rb +1 -0
- data/lib/active_support/cache/moneta_store.rb +102 -6
- data/lib/moneta/adapter.rb +52 -0
- data/lib/moneta/adapters/activerecord.rb +284 -99
- data/lib/moneta/adapters/activesupportcache.rb +120 -0
- data/lib/moneta/adapters/cassandra.rb +303 -51
- data/lib/moneta/adapters/client.rb +72 -23
- data/lib/moneta/adapters/couch.rb +353 -49
- data/lib/moneta/adapters/datamapper.rb +11 -5
- data/lib/moneta/adapters/daybreak.rb +31 -20
- data/lib/moneta/adapters/dbm.rb +10 -16
- data/lib/moneta/adapters/file.rb +34 -13
- data/lib/moneta/adapters/fog.rb +5 -6
- data/lib/moneta/adapters/gdbm.rb +10 -16
- data/lib/moneta/adapters/hbase.rb +11 -13
- data/lib/moneta/adapters/kyotocabinet.rb +78 -17
- data/lib/moneta/adapters/leveldb.rb +35 -15
- data/lib/moneta/adapters/lmdb.rb +47 -23
- data/lib/moneta/adapters/localmemcache.rb +7 -13
- data/lib/moneta/adapters/lruhash.rb +45 -48
- data/lib/moneta/adapters/memcached/dalli.rb +67 -30
- data/lib/moneta/adapters/memcached/native.rb +33 -30
- data/lib/moneta/adapters/memcached.rb +1 -0
- data/lib/moneta/adapters/memory.rb +7 -7
- data/lib/moneta/adapters/mongo.rb +265 -6
- data/lib/moneta/adapters/null.rb +1 -2
- data/lib/moneta/adapters/pstore.rb +68 -25
- data/lib/moneta/adapters/redis.rb +101 -32
- data/lib/moneta/adapters/restclient.rb +17 -16
- data/lib/moneta/adapters/riak.rb +10 -11
- data/lib/moneta/adapters/sdbm.rb +10 -16
- data/lib/moneta/adapters/sequel/mysql.rb +66 -0
- data/lib/moneta/adapters/sequel/postgres.rb +80 -0
- data/lib/moneta/adapters/sequel/postgres_hstore.rb +240 -0
- data/lib/moneta/adapters/sequel/sqlite.rb +57 -0
- data/lib/moneta/adapters/sequel.rb +253 -52
- data/lib/moneta/adapters/sqlite.rb +103 -23
- data/lib/moneta/adapters/tdb.rb +21 -12
- data/lib/moneta/adapters/tokyocabinet.rb +24 -16
- data/lib/moneta/adapters/tokyotyrant.rb +49 -28
- data/lib/moneta/adapters/yaml.rb +1 -5
- data/lib/moneta/builder.rb +2 -3
- data/lib/moneta/cache.rb +11 -1
- data/lib/moneta/config.rb +101 -0
- data/lib/moneta/create_support.rb +21 -0
- data/lib/moneta/dbm_adapter.rb +31 -0
- data/lib/moneta/defaults.rb +320 -0
- data/lib/moneta/each_key_support.rb +27 -0
- data/lib/moneta/enumerable.rb +38 -0
- data/lib/moneta/expires.rb +112 -11
- data/lib/moneta/expires_support.rb +59 -0
- data/lib/moneta/fallback.rb +84 -0
- data/lib/moneta/hash_adapter.rb +68 -0
- data/lib/moneta/increment_support.rb +16 -0
- data/lib/moneta/lock.rb +26 -2
- data/lib/moneta/logger.rb +2 -2
- data/lib/moneta/nil_values.rb +35 -0
- data/lib/moneta/option_support.rb +51 -0
- data/lib/moneta/optionmerger.rb +0 -1
- data/lib/moneta/pool.rb +339 -28
- data/lib/moneta/proxy.rb +75 -2
- data/lib/moneta/server.rb +230 -68
- data/lib/moneta/shared.rb +36 -14
- data/lib/moneta/stack.rb +7 -7
- data/lib/moneta/synchronize.rb +5 -11
- data/lib/moneta/transformer/config.rb +64 -42
- data/lib/moneta/transformer/helper/bson.rb +7 -14
- data/lib/moneta/transformer/helper.rb +5 -5
- data/lib/moneta/transformer.rb +217 -23
- data/lib/moneta/utils.rb +3 -9
- data/lib/moneta/version.rb +1 -1
- data/lib/moneta/weak_each_key.rb +72 -0
- data/lib/moneta/wrapper.rb +30 -0
- data/lib/moneta.rb +29 -10
- data/lib/rack/cache/moneta.rb +13 -11
- data/lib/rack/moneta_rest.rb +2 -2
- data/lib/rack/session/moneta.rb +3 -4
- data/moneta.gemspec +19 -4
- data/script/benchmarks +311 -100
- data/script/contributors +11 -6
- data/script/parallel-tests +58 -83
- data/script/start-couchdb +27 -0
- data/script/start-hbase +47 -0
- data/script/start-services +5 -6
- data/script/update-feature-matrix +148 -0
- data/spec/active_support/cache_moneta_store_spec.rb +129 -106
- data/spec/features/concurrent_create.rb +42 -0
- data/spec/features/concurrent_increment.rb +39 -0
- data/spec/features/create.rb +25 -0
- data/spec/features/create_expires.rb +19 -0
- data/spec/features/default_expires.rb +14 -0
- data/spec/features/each_key.rb +119 -0
- data/spec/features/expires.rb +296 -0
- data/spec/features/features.rb +16 -0
- data/spec/features/increment.rb +98 -0
- data/spec/features/marshallable_key.rb +43 -0
- data/spec/features/marshallable_value.rb +7 -0
- data/spec/features/multiprocess.rb +8 -0
- data/spec/features/not_create.rb +7 -0
- data/spec/features/not_each_key.rb +7 -0
- data/spec/features/not_increment.rb +13 -0
- data/spec/features/not_persist.rb +9 -0
- data/spec/features/null.rb +66 -0
- data/spec/features/persist.rb +14 -0
- data/spec/features/returndifferent.rb +9 -0
- data/spec/features/returnsame.rb +9 -0
- data/spec/features/store.rb +261 -0
- data/spec/features/store_large.rb +13 -0
- data/spec/features/transform_value.rb +44 -0
- data/spec/features/transform_value_expires.rb +41 -0
- data/spec/helper.rb +345 -78
- data/spec/moneta/adapters/activerecord/adapter_activerecord_existing_connection_spec.rb +61 -0
- data/spec/moneta/adapters/activerecord/adapter_activerecord_spec.rb +126 -0
- data/spec/moneta/adapters/activerecord/standard_activerecord_spec.rb +21 -0
- data/spec/moneta/adapters/activerecord/standard_activerecord_with_expires_spec.rb +27 -0
- data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +54 -0
- data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +54 -0
- data/spec/moneta/adapters/activesupportcache/standard_activesupportcache_spec.rb +14 -0
- data/spec/moneta/adapters/cassandra/adapter_cassandra_spec.rb +17 -0
- data/spec/moneta/adapters/cassandra/adapter_cassandra_with_default_expires_spec.rb +18 -0
- data/spec/moneta/adapters/cassandra/helper.rb +18 -0
- data/spec/moneta/adapters/cassandra/standard_cassandra_spec.rb +18 -0
- data/spec/moneta/adapters/client/adapter_client_spec.rb +11 -0
- data/spec/moneta/adapters/client/client_helper.rb +25 -0
- data/spec/moneta/adapters/client/standard_client_tcp_spec.rb +23 -0
- data/spec/moneta/adapters/client/standard_client_unix_spec.rb +28 -0
- data/spec/moneta/adapters/cookie/adapter_cookie_spec.rb +7 -0
- data/spec/moneta/adapters/couch/adapter_couch_spec.rb +204 -0
- data/spec/moneta/adapters/couch/standard_couch_spec.rb +15 -0
- data/spec/moneta/adapters/couch/standard_couch_with_expires_spec.rb +19 -0
- data/spec/moneta/adapters/datamapper/adapter_datamapper_spec.rb +60 -0
- data/spec/moneta/adapters/datamapper/standard_datamapper_spec.rb +21 -0
- data/spec/moneta/adapters/datamapper/standard_datamapper_with_expires_spec.rb +26 -0
- data/spec/moneta/adapters/datamapper/standard_datamapper_with_repository_spec.rb +22 -0
- data/spec/moneta/adapters/daybreak/adapter_daybreak_spec.rb +7 -0
- data/spec/moneta/adapters/daybreak/standard_daybreak_spec.rb +7 -0
- data/spec/moneta/adapters/daybreak/standard_daybreak_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/dbm/adapter_dbm_spec.rb +7 -0
- data/spec/moneta/adapters/dbm/standard_dbm_spec.rb +7 -0
- data/spec/moneta/adapters/dbm/standard_dbm_with_expires_spec.rb +12 -0
- data/spec/moneta/adapters/faraday_helper.rb +10 -0
- data/spec/moneta/adapters/file/adapter_file_spec.rb +7 -0
- data/spec/moneta/adapters/file/standard_file_spec.rb +7 -0
- data/spec/moneta/adapters/file/standard_file_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/fog/adapter_fog_spec.rb +16 -0
- data/spec/moneta/adapters/fog/standard_fog_spec.rb +15 -0
- data/spec/moneta/adapters/fog/standard_fog_with_expires_spec.rb +20 -0
- data/spec/moneta/adapters/gdbm/adapter_gdbm_spec.rb +7 -0
- data/spec/moneta/adapters/gdbm/standard_gdbm_spec.rb +7 -0
- data/spec/moneta/adapters/gdbm/standard_gdbm_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/hashfile/standard_hashfile_spec.rb +7 -0
- data/spec/moneta/adapters/hashfile/standard_hashfile_with_expires_spec.rb +12 -0
- data/spec/moneta/adapters/hbase/adapter_hbase_spec.rb +7 -0
- data/spec/moneta/adapters/hbase/standard_hbase_spec.rb +4 -0
- data/spec/moneta/adapters/hbase/standard_hbase_with_expires_spec.rb +8 -0
- data/spec/moneta/adapters/kyotocabinet/adapter_kyotocabinet_spec.rb +7 -0
- data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_spec.rb +7 -0
- data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_with_expires_spec.rb +14 -0
- data/spec/moneta/adapters/leveldb/adapter_leveldb_spec.rb +7 -0
- data/spec/moneta/adapters/leveldb/standard_leveldb_spec.rb +7 -0
- data/spec/moneta/adapters/leveldb/standard_leveldb_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/lmdb/adapter_lmdb_spec.rb +7 -0
- data/spec/moneta/adapters/lmdb/adapter_lmdb_with_db_spec.rb +7 -0
- data/spec/moneta/adapters/lmdb/standard_lmdb_spec.rb +7 -0
- data/spec/moneta/adapters/lmdb/standard_lmdb_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/localmemcache/adapter_localmemcache_spec.rb +7 -0
- data/spec/moneta/adapters/localmemcache/standard_localmemcache_spec.rb +7 -0
- data/spec/moneta/adapters/localmemcache/standard_localmemcache_with_expires_spec.rb +11 -0
- data/spec/moneta/{adapter_lruhash_spec.rb → adapters/lruhash/adapter_lruhash_spec.rb} +15 -33
- data/spec/moneta/adapters/lruhash/standard_lruhash_spec.rb +4 -0
- data/spec/moneta/adapters/lruhash/standard_lruhash_with_expires_spec.rb +8 -0
- data/spec/moneta/adapters/memcached/adapter_memcached_spec.rb +13 -0
- data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +25 -0
- data/spec/moneta/adapters/memcached/dalli/standard_memcached_dalli_spec.rb +11 -0
- data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +25 -0
- data/spec/moneta/adapters/memcached/native/standard_memcached_native_spec.rb +11 -0
- data/spec/moneta/adapters/memcached/standard_memcached_spec.rb +17 -0
- data/spec/moneta/adapters/memcached_helper.rb +20 -0
- data/spec/moneta/adapters/memory/adapter_memory_spec.rb +7 -0
- data/spec/moneta/adapters/memory/standard_memory_spec.rb +4 -0
- data/spec/moneta/adapters/memory/standard_memory_with_compress_spec.rb +9 -0
- data/spec/moneta/adapters/memory/standard_memory_with_expires_spec.rb +8 -0
- data/spec/moneta/adapters/memory/standard_memory_with_json_key_serializer_spec.rb +4 -0
- data/spec/moneta/adapters/memory/standard_memory_with_json_serializer_spec.rb +9 -0
- data/spec/moneta/adapters/memory/standard_memory_with_json_value_serializer_spec.rb +9 -0
- data/spec/moneta/adapters/memory/standard_memory_with_prefix_spec.rb +41 -0
- data/spec/moneta/adapters/memory/standard_memory_with_snappy_compress_spec.rb +9 -0
- data/spec/moneta/adapters/mongo/adapter_mongo_spec.rb +41 -0
- data/spec/moneta/adapters/mongo/adapter_mongo_with_default_expires_spec.rb +14 -0
- data/spec/moneta/adapters/mongo/standard_mongo_spec.rb +7 -0
- data/spec/moneta/adapters/null/null_adapter_spec.rb +7 -0
- data/spec/moneta/adapters/null/standard_null_spec.rb +4 -0
- data/spec/moneta/adapters/pstore/adapter_pstore_spec.rb +7 -0
- data/spec/moneta/adapters/pstore/standard_pstore_spec.rb +9 -0
- data/spec/moneta/adapters/pstore/standard_pstore_with_expires_spec.rb +13 -0
- data/spec/moneta/adapters/redis/adapter_redis_spec.rb +20 -0
- data/spec/moneta/adapters/redis/standard_redis_spec.rb +14 -0
- data/spec/moneta/adapters/restclient/adapter_restclient_spec.rb +13 -0
- data/spec/moneta/adapters/restclient/helper.rb +12 -0
- data/spec/moneta/adapters/restclient/standard_restclient_spec.rb +13 -0
- data/spec/moneta/adapters/riak/adapter_riak_spec.rb +14 -0
- data/spec/moneta/adapters/riak/standard_riak_spec.rb +10 -0
- data/spec/moneta/adapters/riak/standard_riak_with_expires_spec.rb +14 -0
- data/spec/moneta/adapters/sdbm/adapter_sdbm_spec.rb +7 -0
- data/spec/moneta/adapters/sdbm/standard_sdbm_spec.rb +7 -0
- data/spec/moneta/adapters/sdbm/standard_sdbm_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/sequel/adapter_sequel_spec.rb +133 -0
- data/spec/moneta/adapters/sequel/helper.rb +80 -0
- data/spec/moneta/adapters/sequel/standard_sequel_spec.rb +8 -0
- data/spec/moneta/adapters/sequel/standard_sequel_with_expires_spec.rb +18 -0
- data/spec/moneta/adapters/sqlite/adapter_sqlite_spec.rb +7 -0
- data/spec/moneta/adapters/sqlite/standard_sqlite_spec.rb +7 -0
- data/spec/moneta/adapters/sqlite/standard_sqlite_with_expires_spec.rb +15 -0
- data/spec/moneta/adapters/tdb/adapter_tdb_spec.rb +7 -0
- data/spec/moneta/adapters/tdb/standard_tdb_spec.rb +7 -0
- data/spec/moneta/adapters/tdb/standard_tdb_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/tokyocabinet/adapter_tokyocabinet_bdb_spec.rb +7 -0
- data/spec/moneta/adapters/tokyocabinet/adapter_tokyocabinet_hdb_spec.rb +7 -0
- data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_spec.rb +7 -0
- data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_with_expires_spec.rb +11 -0
- data/spec/moneta/adapters/tokyotyrant/adapter_tokyotyrant_spec.rb +11 -0
- data/spec/moneta/adapters/tokyotyrant/helper.rb +12 -0
- data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_spec.rb +7 -0
- data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_with_expires_spec.rb +12 -0
- data/spec/moneta/adapters/yaml/adapter_yaml_spec.rb +7 -0
- data/spec/moneta/adapters/yaml/standard_yaml_spec.rb +9 -0
- data/spec/moneta/adapters/yaml/standard_yaml_with_expires_spec.rb +13 -0
- data/spec/moneta/builder_spec.rb +22 -1
- data/spec/moneta/config_spec.rb +219 -0
- data/spec/moneta/mutex_spec.rb +6 -23
- data/spec/moneta/proxies/cache/cache_file_memory_spec.rb +24 -0
- data/spec/moneta/proxies/cache/cache_memory_null_spec.rb +12 -0
- data/spec/moneta/proxies/enumerable/enumerable_spec.rb +26 -0
- data/spec/moneta/proxies/expires/expires_file_spec.rb +28 -0
- data/spec/moneta/proxies/expires/expires_memory_spec.rb +15 -0
- data/spec/moneta/proxies/expires/expires_memory_with_default_expires_spec.rb +16 -0
- data/spec/moneta/proxies/fallback/fallback_spec.rb +42 -0
- data/spec/moneta/proxies/lock/lock_spec.rb +10 -0
- data/spec/moneta/{optionmerger_spec.rb → proxies/optionmerger/optionmerger_spec.rb} +2 -18
- data/spec/moneta/proxies/pool/pool_spec.rb +353 -0
- data/spec/moneta/proxies/proxy/proxy_expires_memory_spec.rb +16 -0
- data/spec/moneta/proxies/shared/shared_tcp_spec.rb +59 -0
- data/spec/moneta/proxies/shared/shared_unix_spec.rb +58 -0
- data/spec/moneta/proxies/transformer/transformer_bencode_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_bert_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_bson_spec.rb +25 -0
- data/spec/moneta/proxies/transformer/transformer_bzip2_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_json_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_key_inspect_spec.rb +17 -0
- data/spec/moneta/proxies/transformer/transformer_key_marshal_spec.rb +17 -0
- data/spec/moneta/proxies/transformer/transformer_key_to_s_spec.rb +17 -0
- data/spec/moneta/proxies/transformer/transformer_key_yaml_spec.rb +17 -0
- data/spec/moneta/proxies/transformer/transformer_lz4_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_lzma_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_lzo_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_base64_spec.rb +20 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_city128_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_city32_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_city64_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_escape_spec.rb +21 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_hex_spec.rb +20 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_hmac_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_md5_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_md5_spread_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_prefix_base64_spec.rb +33 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_prefix_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_qp_spec.rb +20 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_rmd160_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_sha1_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_sha256_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_sha384_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_sha512_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_truncate_spec.rb +15 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_urlsafe_base64_spec.rb +20 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_uuencode_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_msgpack_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_ox_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_php_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_quicklz_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_snappy_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_tnet_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_value_marshal_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_value_yaml_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_yaml_spec.rb +19 -0
- data/spec/moneta/proxies/transformer/transformer_zlib_spec.rb +19 -0
- data/spec/moneta/proxies/weak_create/weak_create_spec.rb +21 -0
- data/spec/moneta/proxies/weak_each_key/weak_each_key_spec.rb +22 -0
- data/spec/moneta/proxies/weak_increment/weak_increment_spec.rb +21 -0
- data/spec/moneta/semaphore_spec.rb +6 -23
- data/spec/moneta/stack_file_memory_spec.rb +5 -29
- data/spec/moneta/stack_memory_file_spec.rb +5 -31
- data/spec/rack/session_moneta_spec.rb +44 -25
- data/spec/restserver.rb +44 -0
- data/{spec → test}/action_dispatch/fixtures/session_autoload_test/foo.rb +0 -0
- data/{spec/action_dispatch/session_moneta_store_spec.rb → test/action_dispatch/session_moneta_store_test.rb} +13 -5
- metadata +545 -369
- data/.travis.yml +0 -55
- data/lib/moneta/adapters/mongo/base.rb +0 -78
- data/lib/moneta/adapters/mongo/moped.rb +0 -107
- data/lib/moneta/adapters/mongo/official.rb +0 -117
- data/lib/moneta/mixins.rb +0 -343
- data/script/generate-specs +0 -2696
- data/script/install-kyotocabinet +0 -17
- data/spec/moneta/adapter_activerecord_exisiting_connection_spec.rb +0 -36
- data/spec/moneta/adapter_activerecord_spec.rb +0 -65
- data/spec/moneta/adapter_cassandra_spec.rb +0 -33
- data/spec/moneta/adapter_cassandra_with_default_expires_spec.rb +0 -34
- data/spec/moneta/adapter_client_spec.rb +0 -35
- data/spec/moneta/adapter_cookie_spec.rb +0 -30
- data/spec/moneta/adapter_couch_spec.rb +0 -36
- data/spec/moneta/adapter_datamapper_spec.rb +0 -64
- data/spec/moneta/adapter_daybreak_spec.rb +0 -31
- data/spec/moneta/adapter_dbm_spec.rb +0 -31
- data/spec/moneta/adapter_file_spec.rb +0 -34
- data/spec/moneta/adapter_fog_spec.rb +0 -37
- data/spec/moneta/adapter_gdbm_spec.rb +0 -31
- data/spec/moneta/adapter_hbase_spec.rb +0 -33
- data/spec/moneta/adapter_kyotocabinet_spec.rb +0 -31
- data/spec/moneta/adapter_leveldb_spec.rb +0 -31
- data/spec/moneta/adapter_lmdb_spec.rb +0 -32
- data/spec/moneta/adapter_lmdb_with_db_spec.rb +0 -32
- data/spec/moneta/adapter_localmemcache_spec.rb +0 -32
- data/spec/moneta/adapter_memcached_dalli_spec.rb +0 -36
- data/spec/moneta/adapter_memcached_dalli_with_default_expires_spec.rb +0 -37
- data/spec/moneta/adapter_memcached_native_spec.rb +0 -36
- data/spec/moneta/adapter_memcached_native_with_default_expires_spec.rb +0 -37
- data/spec/moneta/adapter_memcached_spec.rb +0 -36
- data/spec/moneta/adapter_memcached_with_default_expires_spec.rb +0 -37
- data/spec/moneta/adapter_memory_spec.rb +0 -184
- data/spec/moneta/adapter_mongo_moped_spec.rb +0 -64
- data/spec/moneta/adapter_mongo_moped_with_default_expires_spec.rb +0 -53
- data/spec/moneta/adapter_mongo_official_spec.rb +0 -64
- data/spec/moneta/adapter_mongo_official_with_default_expires_spec.rb +0 -53
- data/spec/moneta/adapter_mongo_spec.rb +0 -51
- data/spec/moneta/adapter_mongo_with_default_expires_spec.rb +0 -53
- data/spec/moneta/adapter_pstore_spec.rb +0 -250
- data/spec/moneta/adapter_redis_spec.rb +0 -36
- data/spec/moneta/adapter_redis_with_default_expires_spec.rb +0 -37
- data/spec/moneta/adapter_restclient_spec.rb +0 -33
- data/spec/moneta/adapter_riak_spec.rb +0 -36
- data/spec/moneta/adapter_sdbm_spec.rb +0 -30
- data/spec/moneta/adapter_sequel_spec.rb +0 -34
- data/spec/moneta/adapter_sqlite_spec.rb +0 -32
- data/spec/moneta/adapter_tdb_spec.rb +0 -31
- data/spec/moneta/adapter_tokyocabinet_bdb_spec.rb +0 -31
- data/spec/moneta/adapter_tokyocabinet_hdb_spec.rb +0 -31
- data/spec/moneta/adapter_tokyotyrant_spec.rb +0 -34
- data/spec/moneta/adapter_yaml_spec.rb +0 -57
- data/spec/moneta/cache_file_memory_spec.rb +0 -50
- data/spec/moneta/cache_memory_null_spec.rb +0 -35
- data/spec/moneta/expires_file_spec.rb +0 -78
- data/spec/moneta/expires_memory_spec.rb +0 -189
- data/spec/moneta/expires_memory_with_default_expires_spec.rb +0 -190
- data/spec/moneta/lock_spec.rb +0 -187
- data/spec/moneta/null_adapter_spec.rb +0 -86
- data/spec/moneta/pool_spec.rb +0 -38
- data/spec/moneta/proxy_expires_memory_spec.rb +0 -191
- data/spec/moneta/proxy_redis_spec.rb +0 -40
- data/spec/moneta/shared_tcp_spec.rb +0 -46
- data/spec/moneta/shared_unix_spec.rb +0 -46
- data/spec/moneta/standard_activerecord_spec.rb +0 -253
- data/spec/moneta/standard_activerecord_with_expires_spec.rb +0 -255
- data/spec/moneta/standard_cassandra_spec.rb +0 -252
- data/spec/moneta/standard_client_tcp_spec.rb +0 -269
- data/spec/moneta/standard_client_unix_spec.rb +0 -254
- data/spec/moneta/standard_couch_spec.rb +0 -252
- data/spec/moneta/standard_couch_with_expires_spec.rb +0 -254
- data/spec/moneta/standard_datamapper_spec.rb +0 -254
- data/spec/moneta/standard_datamapper_with_expires_spec.rb +0 -256
- data/spec/moneta/standard_datamapper_with_repository_spec.rb +0 -254
- data/spec/moneta/standard_daybreak_spec.rb +0 -250
- data/spec/moneta/standard_daybreak_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_dbm_spec.rb +0 -250
- data/spec/moneta/standard_dbm_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_file_spec.rb +0 -253
- data/spec/moneta/standard_file_with_expires_spec.rb +0 -255
- data/spec/moneta/standard_fog_spec.rb +0 -256
- data/spec/moneta/standard_fog_with_expires_spec.rb +0 -258
- data/spec/moneta/standard_gdbm_spec.rb +0 -250
- data/spec/moneta/standard_gdbm_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_hashfile_spec.rb +0 -253
- data/spec/moneta/standard_hashfile_with_expires_spec.rb +0 -255
- data/spec/moneta/standard_hbase_spec.rb +0 -252
- data/spec/moneta/standard_hbase_with_expires_spec.rb +0 -255
- data/spec/moneta/standard_kyotocabinet_spec.rb +0 -250
- data/spec/moneta/standard_kyotocabinet_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_leveldb_spec.rb +0 -250
- data/spec/moneta/standard_leveldb_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_lmdb_spec.rb +0 -251
- data/spec/moneta/standard_lmdb_with_expires_spec.rb +0 -253
- data/spec/moneta/standard_localmemcache_spec.rb +0 -251
- data/spec/moneta/standard_localmemcache_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_lruhash_spec.rb +0 -187
- data/spec/moneta/standard_lruhash_with_expires_spec.rb +0 -189
- data/spec/moneta/standard_memcached_dalli_spec.rb +0 -255
- data/spec/moneta/standard_memcached_native_spec.rb +0 -255
- data/spec/moneta/standard_memcached_spec.rb +0 -255
- data/spec/moneta/standard_memory_spec.rb +0 -187
- data/spec/moneta/standard_memory_with_compress_spec.rb +0 -187
- data/spec/moneta/standard_memory_with_expires_spec.rb +0 -189
- data/spec/moneta/standard_memory_with_json_key_serializer_spec.rb +0 -86
- data/spec/moneta/standard_memory_with_json_serializer_spec.rb +0 -49
- data/spec/moneta/standard_memory_with_json_value_serializer_spec.rb +0 -90
- data/spec/moneta/standard_memory_with_prefix_spec.rb +0 -187
- data/spec/moneta/standard_memory_with_snappy_compress_spec.rb +0 -187
- data/spec/moneta/standard_mongo_moped_spec.rb +0 -255
- data/spec/moneta/standard_mongo_official_spec.rb +0 -255
- data/spec/moneta/standard_mongo_spec.rb +0 -255
- data/spec/moneta/standard_null_spec.rb +0 -120
- data/spec/moneta/standard_pstore_spec.rb +0 -253
- data/spec/moneta/standard_pstore_with_expires_spec.rb +0 -255
- data/spec/moneta/standard_redis_spec.rb +0 -255
- data/spec/moneta/standard_restclient_spec.rb +0 -252
- data/spec/moneta/standard_riak_spec.rb +0 -255
- data/spec/moneta/standard_riak_with_expires_spec.rb +0 -256
- data/spec/moneta/standard_sdbm_spec.rb +0 -249
- data/spec/moneta/standard_sdbm_with_expires_spec.rb +0 -251
- data/spec/moneta/standard_sequel_spec.rb +0 -253
- data/spec/moneta/standard_sequel_with_expires_spec.rb +0 -255
- data/spec/moneta/standard_sqlite_spec.rb +0 -251
- data/spec/moneta/standard_sqlite_with_expires_spec.rb +0 -253
- data/spec/moneta/standard_tdb_spec.rb +0 -250
- data/spec/moneta/standard_tdb_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_tokyocabinet_spec.rb +0 -250
- data/spec/moneta/standard_tokyocabinet_with_expires_spec.rb +0 -252
- data/spec/moneta/standard_tokyotyrant_spec.rb +0 -253
- data/spec/moneta/standard_tokyotyrant_with_expires_spec.rb +0 -255
- data/spec/moneta/standard_yaml_spec.rb +0 -250
- data/spec/moneta/standard_yaml_with_expires_spec.rb +0 -252
- data/spec/moneta/transformer_bencode_spec.rb +0 -55
- data/spec/moneta/transformer_bert_spec.rb +0 -55
- data/spec/moneta/transformer_bson_spec.rb +0 -60
- data/spec/moneta/transformer_bzip2_spec.rb +0 -55
- data/spec/moneta/transformer_json_spec.rb +0 -55
- data/spec/moneta/transformer_key_inspect_spec.rb +0 -91
- data/spec/moneta/transformer_key_marshal_spec.rb +0 -191
- data/spec/moneta/transformer_key_to_s_spec.rb +0 -91
- data/spec/moneta/transformer_key_yaml_spec.rb +0 -191
- data/spec/moneta/transformer_lz4_spec.rb +0 -55
- data/spec/moneta/transformer_lzma_spec.rb +0 -55
- data/spec/moneta/transformer_lzo_spec.rb +0 -55
- data/spec/moneta/transformer_marshal_base64_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_city128_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_city32_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_city64_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_escape_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_hex_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_hmac_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_md5_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_md5_spread_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_prefix_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_qp_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_rmd160_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_sha1_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_sha256_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_sha384_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_sha512_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_spec.rb +0 -191
- data/spec/moneta/transformer_marshal_truncate_spec.rb +0 -194
- data/spec/moneta/transformer_marshal_uuencode_spec.rb +0 -194
- data/spec/moneta/transformer_msgpack_spec.rb +0 -55
- data/spec/moneta/transformer_ox_spec.rb +0 -191
- data/spec/moneta/transformer_php_spec.rb +0 -55
- data/spec/moneta/transformer_quicklz_spec.rb +0 -55
- data/spec/moneta/transformer_snappy_spec.rb +0 -55
- data/spec/moneta/transformer_tnet_spec.rb +0 -55
- data/spec/moneta/transformer_value_marshal_spec.rb +0 -191
- data/spec/moneta/transformer_value_yaml_spec.rb +0 -191
- data/spec/moneta/transformer_yaml_spec.rb +0 -191
- data/spec/moneta/transformer_zlib_spec.rb +0 -55
- data/spec/moneta/weak_create_spec.rb +0 -41
- data/spec/moneta/weak_increment_spec.rb +0 -41
- data/spec/monetaspecs.rb +0 -51267
- data/spec/quality_spec.rb +0 -51
@@ -1,11 +1,270 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
|
1
3
|
module Moneta
|
2
4
|
module Adapters
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
# MongoDB backend
|
6
|
+
#
|
7
|
+
# Supports expiration, documents will be automatically removed starting
|
8
|
+
# with mongodb >= 2.2 (see {http://docs.mongodb.org/manual/tutorial/expire-data/}).
|
9
|
+
#
|
10
|
+
# You can store hashes directly using this adapter.
|
11
|
+
#
|
12
|
+
# @example Store hashes
|
13
|
+
# db = Moneta::Adapters::MongoOfficial.new
|
14
|
+
# db['key'] = {a: 1, b: 2}
|
15
|
+
#
|
16
|
+
# @api public
|
17
|
+
class Mongo < Adapter
|
18
|
+
include ExpiresSupport
|
19
|
+
|
20
|
+
supports :each_key, :create, :increment
|
21
|
+
|
22
|
+
config :collection, default: 'moneta'
|
23
|
+
|
24
|
+
config :db
|
25
|
+
config :database, default: 'moneta' do |database:, db:, **|
|
26
|
+
if db
|
27
|
+
warn('Moneta::Adapters::Mongo - the :db option is deprecated and will be removed in a future version. Use :database instead')
|
28
|
+
db
|
29
|
+
else
|
30
|
+
database
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
config :expires_field, default: 'expiresAt'
|
35
|
+
config :value_field, default: 'value'
|
36
|
+
config :type_field, default: 'type'
|
37
|
+
|
38
|
+
backend do |host: '127.0.0.1', port: 27017, **options|
|
39
|
+
options[:logger] ||= ::Logger.new(STDERR).tap do |logger|
|
40
|
+
logger.level = ::Logger::ERROR
|
41
|
+
end
|
42
|
+
::Mongo::Client.new(["#{host}:#{port}"], options)
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param [Hash] options
|
46
|
+
# @option options [String] :collection ('moneta') MongoDB collection name
|
47
|
+
# @option options [String] :host ('127.0.0.1') MongoDB server host
|
48
|
+
# @option options [String] :user Username used to authenticate
|
49
|
+
# @option options [String] :password Password used to authenticate
|
50
|
+
# @option options [Integer] :port (MongoDB default port) MongoDB server port
|
51
|
+
# @option options [String] :database ('moneta') MongoDB database
|
52
|
+
# @option options [Integer] :expires Default expiration time
|
53
|
+
# @option options [String] :expires_field ('expiresAt') Document field to store expiration time
|
54
|
+
# @option options [String] :value_field ('value') Document field to store value
|
55
|
+
# @option options [String] :type_field ('type') Document field to store value type
|
56
|
+
# @option options [::Mongo::Client] :backend Use existing backend instance
|
57
|
+
# @option options Other options passed to `Mongo::MongoClient#new`
|
58
|
+
def initialize(options = {})
|
59
|
+
super
|
60
|
+
|
61
|
+
@database = backend.use(config.database)
|
62
|
+
@collection = @database[config.collection]
|
63
|
+
|
64
|
+
if @database.command(buildinfo: 1).documents.first['version'] >= '2.2'
|
65
|
+
@collection.indexes.create_one({ config.expires_field => 1 }, expire_after: 0)
|
66
|
+
else
|
67
|
+
warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# (see Proxy#load)
|
72
|
+
def load(key, options = {})
|
73
|
+
view = @collection.find(:$and => [
|
74
|
+
{ _id: to_binary(key) },
|
75
|
+
not_expired
|
76
|
+
])
|
77
|
+
|
78
|
+
doc = view.limit(1).first
|
79
|
+
|
80
|
+
if doc
|
81
|
+
update_expiry(options, nil) do |expires|
|
82
|
+
view.update_one(:$set => { config.expires_field => expires })
|
83
|
+
end
|
84
|
+
|
85
|
+
doc_to_value(doc)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# (see Proxy#store)
|
90
|
+
def store(key, value, options = {})
|
91
|
+
key = to_binary(key)
|
92
|
+
@collection.replace_one({ _id: key },
|
93
|
+
value_to_doc(key, value, options),
|
94
|
+
upsert: true)
|
95
|
+
value
|
96
|
+
end
|
97
|
+
|
98
|
+
# (see Proxy#each_key)
|
99
|
+
def each_key
|
100
|
+
return enum_for(:each_key) unless block_given?
|
101
|
+
@collection.find.each { |doc| yield from_binary(doc[:_id]) }
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
# (see Proxy#delete)
|
106
|
+
def delete(key, options = {})
|
107
|
+
key = to_binary(key)
|
108
|
+
if doc = @collection.find(_id: key).find_one_and_delete and
|
109
|
+
!doc[config.expires_field] || doc[config.expires_field] >= Time.now
|
110
|
+
doc_to_value(doc)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# (see Proxy#increment)
|
115
|
+
def increment(key, amount = 1, options = {})
|
116
|
+
@collection.find_one_and_update({ :$and => [{ _id: to_binary(key) }, not_expired] },
|
117
|
+
{ :$inc => { config.value_field => amount } },
|
118
|
+
return_document: :after,
|
119
|
+
upsert: true)[config.value_field]
|
120
|
+
rescue ::Mongo::Error::OperationFailure
|
121
|
+
tries ||= 0
|
122
|
+
(tries += 1) < 3 ? retry : raise
|
123
|
+
end
|
124
|
+
|
125
|
+
# (see Proxy#create)
|
126
|
+
def create(key, value, options = {})
|
127
|
+
key = to_binary(key)
|
128
|
+
@collection.insert_one(value_to_doc(key, value, options))
|
129
|
+
true
|
130
|
+
rescue ::Mongo::Error::OperationFailure => error
|
131
|
+
raise unless error.code == 11000 # duplicate key error
|
132
|
+
false
|
133
|
+
end
|
134
|
+
|
135
|
+
# (see Proxy#clear)
|
136
|
+
def clear(options = {})
|
137
|
+
@collection.delete_many
|
138
|
+
self
|
139
|
+
end
|
140
|
+
|
141
|
+
# (see Proxy#close)
|
142
|
+
def close
|
143
|
+
@database.close
|
144
|
+
nil
|
145
|
+
end
|
146
|
+
|
147
|
+
# (see Proxy#slice)
|
148
|
+
def slice(*keys, **options)
|
149
|
+
view = @collection.find(:$and => [
|
150
|
+
{ _id: { :$in => keys.map(&method(:to_binary)) } },
|
151
|
+
not_expired
|
152
|
+
])
|
153
|
+
pairs = view.map { |doc| [from_binary(doc[:_id]), doc_to_value(doc)] }
|
154
|
+
|
155
|
+
update_expiry(options, nil) do |expires|
|
156
|
+
view.update_many(:$set => { config.expires_field => expires })
|
157
|
+
end
|
158
|
+
|
159
|
+
pairs
|
160
|
+
end
|
161
|
+
|
162
|
+
# (see Proxy#merge!)
|
163
|
+
def merge!(pairs, options = {})
|
164
|
+
existing = Hash[slice(*pairs.map { |key, _| key })]
|
165
|
+
update_pairs, insert_pairs = pairs.partition { |key, _| existing.key?(key) }
|
166
|
+
|
167
|
+
@collection.insert_many(insert_pairs.map do |key, value|
|
168
|
+
value_to_doc(to_binary(key), value, options)
|
169
|
+
end)
|
170
|
+
|
171
|
+
update_pairs.each do |key, value|
|
172
|
+
value = yield(key, existing[key], value) if block_given?
|
173
|
+
binary = to_binary(key)
|
174
|
+
@collection.replace_one({ _id: binary }, value_to_doc(binary, value, options))
|
175
|
+
end
|
176
|
+
|
177
|
+
self
|
178
|
+
end
|
179
|
+
|
180
|
+
# (see Proxy#fetch_values)
|
181
|
+
def fetch_values(*keys, **options)
|
182
|
+
return values_at(*keys, **options) unless block_given?
|
183
|
+
hash = Hash[slice(*keys, **options)]
|
184
|
+
keys.map do |key|
|
185
|
+
if hash.key?(key)
|
186
|
+
hash[key]
|
187
|
+
else
|
188
|
+
yield key
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
# (see Proxy#values_at)
|
194
|
+
def values_at(*keys, **options)
|
195
|
+
hash = Hash[slice(*keys, **options)]
|
196
|
+
keys.map { |key| hash[key] }
|
197
|
+
end
|
198
|
+
|
199
|
+
private
|
200
|
+
|
201
|
+
def doc_to_value(doc)
|
202
|
+
case doc[config.type_field]
|
203
|
+
when 'Hash'
|
204
|
+
doc = doc.dup
|
205
|
+
doc.delete('_id')
|
206
|
+
doc.delete(config.type_field)
|
207
|
+
doc.delete(config.expires_field)
|
208
|
+
doc
|
209
|
+
when 'Number'
|
210
|
+
doc[config.value_field]
|
211
|
+
else
|
212
|
+
# In ruby_bson version 2 (and probably up), #to_s no longer returns the binary data
|
213
|
+
from_binary(doc[config.value_field])
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def value_to_doc(key, value, options)
|
218
|
+
case value
|
219
|
+
when Hash
|
220
|
+
value.merge('_id' => key,
|
221
|
+
config.type_field => 'Hash',
|
222
|
+
# expires_field must be a Time object (BSON date datatype)
|
223
|
+
config.expires_field => expires_at(options) || nil)
|
224
|
+
when Float, Integer
|
225
|
+
{ '_id' => key,
|
226
|
+
config.type_field => 'Number',
|
227
|
+
config.value_field => value,
|
228
|
+
# expires_field must be a Time object (BSON date datatype)
|
229
|
+
config.expires_field => expires_at(options) || nil }
|
230
|
+
when String
|
231
|
+
intvalue = value.to_i
|
232
|
+
{ '_id' => key,
|
233
|
+
config.type_field => 'String',
|
234
|
+
config.value_field => intvalue.to_s == value ? intvalue : to_binary(value),
|
235
|
+
# @expires_field must be a Time object (BSON date datatype)
|
236
|
+
config.expires_field => expires_at(options) || nil }
|
237
|
+
else
|
238
|
+
raise ArgumentError, "Invalid value type: #{value.class}"
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
# BSON will use String#force_encoding to make the string 8-bit
|
243
|
+
# ASCII. This could break unicode text so we should dup in this
|
244
|
+
# case, and it also fails with frozen strings.
|
245
|
+
def to_binary(str)
|
246
|
+
str = str.dup if str.frozen? || str.encoding != Encoding::ASCII_8BIT
|
247
|
+
::BSON::Binary.new(str)
|
248
|
+
end
|
249
|
+
|
250
|
+
def from_binary(binary)
|
251
|
+
binary.is_a?(::BSON::Binary) ? binary.data : binary.to_s
|
252
|
+
end
|
253
|
+
|
254
|
+
def not_expired
|
255
|
+
{
|
256
|
+
:$or => [
|
257
|
+
{ config.expires_field => nil },
|
258
|
+
{ config.expires_field => { :$gte => Time.now } }
|
259
|
+
]
|
260
|
+
}
|
261
|
+
end
|
262
|
+
|
263
|
+
def update_expiry(options, default)
|
264
|
+
if (expires = expires_at(options, default)) != nil
|
265
|
+
yield(expires || nil)
|
266
|
+
end
|
267
|
+
end
|
9
268
|
end
|
10
269
|
end
|
11
270
|
end
|
data/lib/moneta/adapters/null.rb
CHANGED
@@ -5,60 +5,72 @@ module Moneta
|
|
5
5
|
module Adapters
|
6
6
|
# PStore backend
|
7
7
|
# @api public
|
8
|
-
class PStore
|
9
|
-
include
|
8
|
+
class PStore < Adapter
|
9
|
+
include NilValues
|
10
10
|
|
11
|
-
supports :create, :increment
|
12
|
-
|
11
|
+
supports :create, :increment, :each_key
|
12
|
+
|
13
|
+
backend do |file:, threadsafe: false|
|
14
|
+
FileUtils.mkpath(::File.dirname(file))
|
15
|
+
::PStore.new(file, threadsafe)
|
16
|
+
end
|
13
17
|
|
14
18
|
# @param [Hash] options
|
15
19
|
# @option options [String] :file PStore file
|
20
|
+
# @option options [Boolean] :threadsafe Makes the PStore thread-safe
|
16
21
|
# @option options [::PStore] :backend Use existing backend instance
|
17
22
|
def initialize(options = {})
|
18
|
-
|
19
|
-
|
20
|
-
raise ArgumentError, 'Option :file is required' unless options[:file]
|
21
|
-
FileUtils.mkpath(::File.dirname(options[:file]))
|
22
|
-
new_store(options)
|
23
|
-
end
|
23
|
+
super
|
24
|
+
@id = "Moneta::Adapters::PStore(#{object_id})"
|
24
25
|
end
|
25
26
|
|
26
27
|
# (see Proxy#key?)
|
27
28
|
def key?(key, options = {})
|
28
|
-
|
29
|
+
transaction(true) { backend.root?(key) }
|
30
|
+
end
|
31
|
+
|
32
|
+
# (see Proxy#each_key)
|
33
|
+
def each_key(&block)
|
34
|
+
return enum_for(:each_key) { transaction(true) { backend.roots.size } } unless block_given?
|
35
|
+
|
36
|
+
transaction(true) do
|
37
|
+
backend.roots.each { |k| yield(k) }
|
38
|
+
end
|
39
|
+
self
|
29
40
|
end
|
30
41
|
|
31
42
|
# (see Proxy#load)
|
32
43
|
def load(key, options = {})
|
33
|
-
|
44
|
+
transaction(true) { backend[key] }
|
34
45
|
end
|
35
46
|
|
36
47
|
# (see Proxy#store)
|
37
48
|
def store(key, value, options = {})
|
38
|
-
|
49
|
+
transaction { backend[key] = value }
|
39
50
|
end
|
40
51
|
|
41
52
|
# (see Proxy#delete)
|
42
53
|
def delete(key, options = {})
|
43
|
-
|
54
|
+
transaction { backend.delete(key) }
|
44
55
|
end
|
45
56
|
|
46
57
|
# (see Proxy#increment)
|
47
58
|
def increment(key, amount = 1, options = {})
|
48
|
-
|
49
|
-
|
50
|
-
|
59
|
+
transaction do
|
60
|
+
existing = backend[key]
|
61
|
+
value = (existing == nil ? 0 : Integer(existing)) + amount
|
62
|
+
backend[key] = value.to_s
|
51
63
|
value
|
52
64
|
end
|
53
65
|
end
|
54
66
|
|
55
67
|
# (see Proxy#create)
|
56
68
|
def create(key, value, options = {})
|
57
|
-
|
58
|
-
if
|
69
|
+
transaction do
|
70
|
+
if backend.root?(key)
|
59
71
|
false
|
60
72
|
else
|
61
|
-
|
73
|
+
backend[key] = value
|
62
74
|
true
|
63
75
|
end
|
64
76
|
end
|
@@ -66,18 +78,49 @@ module Moneta
|
|
66
78
|
|
67
79
|
# (see Proxy#clear)
|
68
80
|
def clear(options = {})
|
69
|
-
|
70
|
-
|
71
|
-
|
81
|
+
transaction do
|
82
|
+
backend.roots.each do |key|
|
83
|
+
backend.delete(key)
|
72
84
|
end
|
73
85
|
end
|
74
86
|
self
|
75
87
|
end
|
76
88
|
|
89
|
+
# (see Proxy#values_at)
|
90
|
+
def values_at(*keys, **options)
|
91
|
+
transaction(true) { super }
|
92
|
+
end
|
93
|
+
|
94
|
+
def fetch_values(*keys, **options)
|
95
|
+
transaction(true) { super }
|
96
|
+
end
|
97
|
+
|
98
|
+
def slice(*keys, **options)
|
99
|
+
transaction(true) { super }
|
100
|
+
end
|
101
|
+
|
102
|
+
def merge!(pairs, options = {})
|
103
|
+
transaction { super }
|
104
|
+
end
|
105
|
+
|
77
106
|
protected
|
78
107
|
|
79
|
-
|
80
|
-
|
108
|
+
class TransactionError < StandardError; end
|
109
|
+
|
110
|
+
def transaction(read_only = false)
|
111
|
+
case Thread.current[@id]
|
112
|
+
when read_only, false
|
113
|
+
yield
|
114
|
+
when true
|
115
|
+
raise TransactionError, "Attempt to start read-write transaction inside a read-only transaction"
|
116
|
+
else
|
117
|
+
begin
|
118
|
+
Thread.current[@id] = read_only
|
119
|
+
backend.transaction(read_only) { yield }
|
120
|
+
ensure
|
121
|
+
Thread.current[@id] = nil
|
122
|
+
end
|
123
|
+
end
|
81
124
|
end
|
82
125
|
end
|
83
126
|
end
|
@@ -4,46 +4,52 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# Redis backend
|
6
6
|
# @api public
|
7
|
-
class Redis
|
8
|
-
include Defaults
|
7
|
+
class Redis < Adapter
|
9
8
|
include ExpiresSupport
|
10
9
|
|
11
|
-
supports :create, :increment
|
12
|
-
attr_reader :backend
|
10
|
+
supports :create, :increment, :each_key
|
13
11
|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
@backend = options[:backend] || ::Redis.new(options)
|
21
|
-
end
|
12
|
+
# @!method initialize(options = {})
|
13
|
+
# @param [Hash] options
|
14
|
+
# @option options [Integer] :expires Default expiration time
|
15
|
+
# @option options [::Redis] :backend Use existing backend instance
|
16
|
+
# @option options Other options passed to `Redis#new`
|
17
|
+
backend { |**options| ::Redis.new(options) }
|
22
18
|
|
23
19
|
# (see Proxy#key?)
|
24
20
|
#
|
25
21
|
# This method considers false and 0 as "no-expire" and every positive
|
26
22
|
# number as a time to live in seconds.
|
27
23
|
def key?(key, options = {})
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
24
|
+
with_expiry_update(key, default: nil, **options) do |pipeline_handle|
|
25
|
+
if pipeline_handle.respond_to?(:exists?)
|
26
|
+
pipeline_handle.exists?(key)
|
27
|
+
else
|
28
|
+
pipeline_handle.exists(key)
|
29
|
+
end
|
33
30
|
end
|
34
31
|
end
|
35
32
|
|
33
|
+
# (see Proxy#each_key)
|
34
|
+
def each_key(&block)
|
35
|
+
return enum_for(:each_key) unless block_given?
|
36
|
+
|
37
|
+
@backend.scan_each { |k| yield(k) }
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
36
41
|
# (see Proxy#load)
|
37
42
|
def load(key, options = {})
|
38
|
-
|
39
|
-
|
40
|
-
|
43
|
+
with_expiry_update(key, default: nil, **options) do |pipeline_handle|
|
44
|
+
pipeline_handle.get(key)
|
45
|
+
end
|
41
46
|
end
|
42
47
|
|
43
48
|
# (see Proxy#store)
|
44
49
|
def store(key, value, options = {})
|
45
50
|
if expires = expires_value(options)
|
46
|
-
|
51
|
+
Numeric === expires and expires = (expires * 1000).to_i
|
52
|
+
@backend.psetex(key, expires, value)
|
47
53
|
else
|
48
54
|
@backend.set(key, value)
|
49
55
|
end
|
@@ -52,17 +58,19 @@ module Moneta
|
|
52
58
|
|
53
59
|
# (see Proxy#delete)
|
54
60
|
def delete(key, options = {})
|
55
|
-
|
56
|
-
|
57
|
-
|
61
|
+
future = nil
|
62
|
+
@backend.pipelined do |pipeline|
|
63
|
+
future = pipeline.get(key)
|
64
|
+
pipeline.del(key)
|
58
65
|
end
|
66
|
+
future.value
|
59
67
|
end
|
60
68
|
|
61
69
|
# (see Proxy#increment)
|
62
70
|
def increment(key, amount = 1, options = {})
|
63
|
-
|
64
|
-
|
65
|
-
|
71
|
+
with_expiry_update(key, **options) do |pipeline_handle|
|
72
|
+
pipeline_handle.incrby(key, amount)
|
73
|
+
end
|
66
74
|
end
|
67
75
|
|
68
76
|
# (see Proxy#clear)
|
@@ -73,8 +81,10 @@ module Moneta
|
|
73
81
|
|
74
82
|
# (see Defaults#create)
|
75
83
|
def create(key, value, options = {})
|
84
|
+
expires = expires_value(options, config.expires)
|
85
|
+
|
76
86
|
if @backend.setnx(key, value)
|
77
|
-
update_expires(key,
|
87
|
+
update_expires(@backend, key, expires)
|
78
88
|
true
|
79
89
|
else
|
80
90
|
false
|
@@ -87,14 +97,73 @@ module Moneta
|
|
87
97
|
nil
|
88
98
|
end
|
89
99
|
|
100
|
+
# (see Defaults#values_at)
|
101
|
+
def values_at(*keys, **options)
|
102
|
+
with_expiry_update(*keys, default: nil, **options) do |pipeline_handle|
|
103
|
+
pipeline_handle.mget(*keys)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# (see Defaults#merge!)
|
108
|
+
def merge!(pairs, options = {})
|
109
|
+
keys = pairs.map { |key, _| key }
|
110
|
+
|
111
|
+
if block_given?
|
112
|
+
old_values = @backend.mget(*keys)
|
113
|
+
updates = pairs.each_with_index.with_object({}) do |(pair, i), updates|
|
114
|
+
old_value = old_values[i]
|
115
|
+
if old_value != nil
|
116
|
+
key, new_value = pair
|
117
|
+
updates[key] = yield(key, old_value, new_value)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
unless updates.empty?
|
121
|
+
pairs = if pairs.respond_to?(:merge)
|
122
|
+
pairs.merge(updates)
|
123
|
+
else
|
124
|
+
Hash[pairs.to_a].merge!(updates)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
with_expiry_update(*keys, **options) do |pipeline_handle|
|
130
|
+
pipeline_handle.mset(*pairs.to_a.flatten(1))
|
131
|
+
end
|
132
|
+
|
133
|
+
self
|
134
|
+
end
|
135
|
+
|
90
136
|
protected
|
91
137
|
|
92
|
-
def update_expires(
|
93
|
-
case expires
|
138
|
+
def update_expires(pipeline_handle, key, expires)
|
139
|
+
case expires
|
94
140
|
when false
|
95
|
-
|
141
|
+
pipeline_handle.persist(key)
|
96
142
|
when Numeric
|
97
|
-
|
143
|
+
pipeline_handle.pexpire(key, (expires * 1000).to_i)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def with_expiry_update(*keys, default: config.expires, **options)
|
148
|
+
expires = expires_value(options, default)
|
149
|
+
if expires == nil
|
150
|
+
yield(@backend)
|
151
|
+
else
|
152
|
+
future = nil
|
153
|
+
@backend.multi do |pipeline|
|
154
|
+
# as of redis 4.6 calling redis methods on the redis client itself
|
155
|
+
# is deprecated in favor of a pipeline handle provided by the
|
156
|
+
# +multi+ call. This will cause in error in redis >= 5.0.
|
157
|
+
#
|
158
|
+
# In order to continue supporting redis versions < 4.6, the following
|
159
|
+
# fallback has been introduced and can be removed once moneta
|
160
|
+
# no longer supports redis < 4.6.
|
161
|
+
|
162
|
+
pipeline_handle = pipeline || @backend
|
163
|
+
future = yield(pipeline_handle)
|
164
|
+
keys.each { |key| update_expires(pipeline_handle, key, expires) }
|
165
|
+
end
|
166
|
+
future.value
|
98
167
|
end
|
99
168
|
end
|
100
169
|
end
|
@@ -4,46 +4,47 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# Moneta rest client backend which works together with {Rack::MonetaRest}
|
6
6
|
# @api public
|
7
|
-
class RestClient
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
class RestClient < Adapter
|
8
|
+
# @!method initialize(options = {})
|
9
|
+
# @param [Hash] options
|
10
|
+
# @option options [String] :url URL
|
11
|
+
# @option options [Symbol] :adapter The adapter to tell Faraday to use
|
12
|
+
# @option options [Faraday::Connection] :backend Use existing backend instance
|
13
|
+
# @option options Other options passed to {Faraday::new} (unless
|
14
|
+
# :backend option is provided).
|
15
|
+
backend do |url:, adapter: nil, **options|
|
16
|
+
::Faraday.new(url, options) do |faraday|
|
17
|
+
faraday.adapter adapter if adapter
|
18
|
+
end
|
18
19
|
end
|
19
20
|
|
20
21
|
# (see Proxy#key?)
|
21
22
|
def key?(key, options = {})
|
22
|
-
|
23
|
+
backend.head(key).status == 200
|
23
24
|
end
|
24
25
|
|
25
26
|
# (see Proxy#load)
|
26
27
|
def load(key, options = {})
|
27
|
-
response =
|
28
|
+
response = backend.get(key)
|
28
29
|
response.status == 200 ? response.body : nil
|
29
30
|
end
|
30
31
|
|
31
32
|
# (see Proxy#store)
|
32
33
|
def store(key, value, options = {})
|
33
|
-
response =
|
34
|
+
response = backend.post(key, value)
|
34
35
|
raise "HTTP error #{response.status}" unless response.status == 200
|
35
36
|
value
|
36
37
|
end
|
37
38
|
|
38
39
|
# (see Proxy#delete)
|
39
40
|
def delete(key, options = {})
|
40
|
-
response =
|
41
|
+
response = backend.delete(key)
|
41
42
|
response.status == 200 ? response.body : nil
|
42
43
|
end
|
43
44
|
|
44
45
|
# (see Proxy#clear)
|
45
46
|
def clear(options = {})
|
46
|
-
|
47
|
+
backend.delete ''
|
47
48
|
self
|
48
49
|
end
|
49
50
|
end
|