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,174 +1,359 @@
|
|
1
1
|
require 'active_record'
|
2
|
-
require '
|
2
|
+
require 'uri'
|
3
3
|
|
4
4
|
module Moneta
|
5
5
|
module Adapters
|
6
6
|
# ActiveRecord as key/value stores
|
7
7
|
# @api public
|
8
|
-
class ActiveRecord
|
9
|
-
|
8
|
+
class ActiveRecord < Adapter
|
9
|
+
supports :create, :increment, :each_key
|
10
10
|
|
11
|
-
supports :create, :increment
|
12
11
|
attr_reader :table
|
12
|
+
delegate :with_connection, to: :connection_pool
|
13
13
|
|
14
|
-
@
|
15
|
-
@table_refcount = {}
|
16
|
-
|
14
|
+
@connection_lock = ::Mutex.new
|
17
15
|
class << self
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
attr_reader :connection_lock
|
17
|
+
delegate :configurations, :configurations=, :connection_handler, to: ::ActiveRecord::Base
|
18
|
+
|
19
|
+
def retrieve_connection_pool(spec_name)
|
20
|
+
connection_handler.retrieve_connection_pool(spec_name.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def establish_connection(spec_name)
|
24
|
+
connection_lock.synchronize do
|
25
|
+
if connection_pool = retrieve_connection_pool(spec_name)
|
26
|
+
connection_pool
|
27
|
+
else
|
28
|
+
connection_handler.establish_connection(spec_name.to_sym)
|
23
29
|
end
|
24
30
|
end
|
25
31
|
end
|
26
32
|
|
27
|
-
def
|
28
|
-
|
29
|
-
$&.unpack('H2' * $&.bytesize).join.upcase
|
30
|
-
end
|
31
|
-
@table_mutex.synchronize do
|
32
|
-
table =
|
33
|
-
if const_defined?(name)
|
34
|
-
const_get(name)
|
35
|
-
else
|
36
|
-
create(name, options)
|
37
|
-
end
|
38
|
-
@table_refcount[table] ||= 0
|
39
|
-
@table_refcount[table] += 1
|
40
|
-
table
|
41
|
-
end
|
33
|
+
def retrieve_or_establish_connection_pool(spec_name)
|
34
|
+
retrieve_connection_pool(spec_name) || establish_connection(spec_name)
|
42
35
|
end
|
36
|
+
end
|
43
37
|
|
44
|
-
|
38
|
+
config :key_column, default: :k
|
39
|
+
config :value_column, default: :v
|
45
40
|
|
46
|
-
|
47
|
-
|
48
|
-
const_set(name, table)
|
49
|
-
table.table_name = options[:table] || 'moneta'
|
50
|
-
table.primary_key = :k
|
41
|
+
backend required: false do |table: :moneta, connection: nil, create_table: nil|
|
42
|
+
@spec = spec_for_connection(connection)
|
51
43
|
|
52
|
-
|
53
|
-
|
54
|
-
table.establish_connection(options[:connection])
|
55
|
-
rescue
|
56
|
-
tries ||= 0
|
57
|
-
(tries += 1) < 3 ? retry : raise
|
58
|
-
end
|
59
|
-
end
|
44
|
+
# Ensure the table name is a symbol.
|
45
|
+
table_name = table.to_sym
|
60
46
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
t.string :k, null: false
|
66
|
-
t.binary :v
|
67
|
-
end
|
68
|
-
conn.add_index(table.table_name, :k, unique: true)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
table
|
73
|
-
rescue
|
74
|
-
remove_const(name)
|
75
|
-
raise
|
47
|
+
if create_table == nil
|
48
|
+
default_create_table(table_name)
|
49
|
+
elsif create_table
|
50
|
+
with_connection(&create_table)
|
76
51
|
end
|
52
|
+
|
53
|
+
@table = ::Arel::Table.new(table_name)
|
54
|
+
|
55
|
+
# backend is only used if there's an existing ActiveRecord model
|
56
|
+
nil
|
77
57
|
end
|
78
58
|
|
79
59
|
# @param [Hash] options
|
80
|
-
# @option options [
|
81
|
-
# @option options [
|
60
|
+
# @option options [Object] :backend A class object inheriting from ActiveRecord::Base to use as a table
|
61
|
+
# @option options [String,Symbol] :table (:moneta) Table name
|
62
|
+
# @option options [Hash/String/Symbol] :connection ActiveRecord connection configuration (`Hash` or `String`), or
|
63
|
+
# symbol giving the name of a Rails connection (e.g. :production)
|
64
|
+
# @option options [Proc, Boolean] :create_table Proc called with a connection if table
|
65
|
+
# needs to be created. Pass false to skip the create table check all together.
|
66
|
+
# @option options [Symbol] :key_column (:k) The name of the column to use for keys
|
67
|
+
# @option options [Symbol] :value_column (:v) The name of the column to use for values
|
82
68
|
def initialize(options = {})
|
83
|
-
|
69
|
+
super
|
70
|
+
|
71
|
+
# If a :backend was provided, use it to set the spec and table
|
72
|
+
if backend
|
73
|
+
@spec = backend.connection_pool.spec
|
74
|
+
@table = ::Arel::Table.new(backend.table_name)
|
75
|
+
end
|
84
76
|
end
|
85
77
|
|
86
78
|
# (see Proxy#key?)
|
87
79
|
def key?(key, options = {})
|
88
|
-
|
89
|
-
|
80
|
+
with_connection do |conn|
|
81
|
+
sel = arel_sel_key(key).project(::Arel.sql('1'))
|
82
|
+
result = conn.select_all(sel)
|
83
|
+
!result.empty?
|
90
84
|
end
|
91
85
|
end
|
92
86
|
|
87
|
+
# (see Proxy#each_key)
|
88
|
+
def each_key(&block)
|
89
|
+
with_connection do |conn|
|
90
|
+
return enum_for(:each_key) { conn.select_value(arel_sel.project(table[config.key_column].count)) } unless block_given?
|
91
|
+
conn.select_values(arel_sel.project(table[config.key_column])).each { |k| yield(k) }
|
92
|
+
end
|
93
|
+
self
|
94
|
+
end
|
95
|
+
|
93
96
|
# (see Proxy#load)
|
94
97
|
def load(key, options = {})
|
95
|
-
|
96
|
-
|
97
|
-
record && record.v
|
98
|
+
with_connection do |conn|
|
99
|
+
conn_sel_value(conn, key)
|
98
100
|
end
|
99
101
|
end
|
100
102
|
|
101
103
|
# (see Proxy#store)
|
102
104
|
def store(key, value, options = {})
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
record.save
|
107
|
-
value
|
105
|
+
with_connection do |conn|
|
106
|
+
encoded = encode(conn, value)
|
107
|
+
conn_ins(conn, key, encoded) unless conn_upd(conn, key, encoded) == 1
|
108
108
|
end
|
109
|
-
|
110
|
-
tries ||= 0
|
111
|
-
(tries += 1) < 10 ? retry : raise
|
109
|
+
value
|
112
110
|
end
|
113
111
|
|
114
112
|
# (see Proxy#delete)
|
115
113
|
def delete(key, options = {})
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
114
|
+
with_connection do |conn|
|
115
|
+
conn.transaction do
|
116
|
+
sel = arel_sel_key(key).project(table[config.value_column]).lock
|
117
|
+
value = decode(conn, conn.select_value(sel))
|
118
|
+
|
119
|
+
del = arel_del.where(table[config.key_column].eq(key))
|
120
|
+
conn.delete(del)
|
121
|
+
|
122
|
+
value
|
120
123
|
end
|
121
124
|
end
|
122
125
|
end
|
123
126
|
|
124
127
|
# (see Proxy#increment)
|
125
128
|
def increment(key, amount = 1, options = {})
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
129
|
+
with_connection do |conn|
|
130
|
+
begin
|
131
|
+
conn_ins(conn, key, amount.to_s)
|
132
|
+
amount
|
133
|
+
rescue ::ActiveRecord::RecordNotUnique
|
134
|
+
conn.transaction do
|
135
|
+
sel = arel_sel_key(key).project(table[config.value_column]).lock
|
136
|
+
value = decode(conn, conn.select_value(sel))
|
137
|
+
value = (value ? Integer(value) : 0) + amount
|
138
|
+
# Re-raise if the upate affects no rows (i.e. row deleted after attempted insert,
|
139
|
+
# before select for update)
|
140
|
+
raise unless conn_upd(conn, key, value.to_s) == 1
|
132
141
|
value
|
133
|
-
elsif create(key, amount.to_s, options)
|
134
|
-
amount
|
135
|
-
else
|
136
|
-
raise 'Concurrent modification'
|
137
142
|
end
|
138
143
|
end
|
139
144
|
end
|
140
|
-
rescue
|
145
|
+
rescue ::ActiveRecord::RecordNotUnique, ::ActiveRecord::Deadlocked
|
146
|
+
# This handles the "no row updated" issue, above, as well as deadlocks
|
147
|
+
# which may occur on some adapters
|
141
148
|
tries ||= 0
|
142
|
-
(tries += 1)
|
149
|
+
(tries += 1) <= 3 ? retry : raise
|
143
150
|
end
|
144
151
|
|
145
152
|
# (see Proxy#create)
|
146
153
|
def create(key, value, options = {})
|
147
|
-
|
148
|
-
|
149
|
-
record.k = key
|
150
|
-
record.v = value
|
151
|
-
record.save
|
154
|
+
with_connection do |conn|
|
155
|
+
conn_ins(conn, key, value)
|
152
156
|
true
|
153
157
|
end
|
154
|
-
rescue
|
155
|
-
# FIXME: This catches too many errors
|
156
|
-
# it should only catch a not-unique-exception
|
158
|
+
rescue ::ActiveRecord::RecordNotUnique
|
157
159
|
false
|
158
160
|
end
|
159
161
|
|
160
162
|
# (see Proxy#clear)
|
161
163
|
def clear(options = {})
|
162
|
-
|
163
|
-
|
164
|
+
with_connection do |conn|
|
165
|
+
conn.delete(arel_del)
|
164
166
|
end
|
165
167
|
self
|
166
168
|
end
|
167
169
|
|
168
170
|
# (see Proxy#close)
|
169
171
|
def close
|
170
|
-
self.class.release(@table)
|
171
172
|
@table = nil
|
173
|
+
@spec = nil
|
174
|
+
end
|
175
|
+
|
176
|
+
# (see Proxy#slice)
|
177
|
+
def slice(*keys, lock: false, **options)
|
178
|
+
with_connection do |conn|
|
179
|
+
conn.create_table(:slice_keys, temporary: true) do |t|
|
180
|
+
t.string :key, null: false
|
181
|
+
end
|
182
|
+
|
183
|
+
begin
|
184
|
+
temp_table = ::Arel::Table.new(:slice_keys)
|
185
|
+
keys.each do |key|
|
186
|
+
conn.insert ::Arel::InsertManager.new
|
187
|
+
.into(temp_table)
|
188
|
+
.insert([[temp_table[:key], key]])
|
189
|
+
end
|
190
|
+
|
191
|
+
sel = arel_sel
|
192
|
+
.join(temp_table)
|
193
|
+
.on(table[config.key_column].eq(temp_table[:key]))
|
194
|
+
.project(table[config.key_column], table[config.value_column])
|
195
|
+
sel = sel.lock if lock
|
196
|
+
result = conn.select_all(sel)
|
197
|
+
|
198
|
+
k = config.key_column.to_s
|
199
|
+
v = config.value_column.to_s
|
200
|
+
result.map do |row|
|
201
|
+
[row[k], decode(conn, row[v])]
|
202
|
+
end
|
203
|
+
ensure
|
204
|
+
conn.drop_table(:slice_keys)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# (see Proxy#values_at)
|
210
|
+
def values_at(*keys, **options)
|
211
|
+
hash = Hash[slice(*keys, **options)]
|
212
|
+
keys.map { |key| hash[key] }
|
213
|
+
end
|
214
|
+
|
215
|
+
# (see Proxy#fetch_values)
|
216
|
+
def fetch_values(*keys, **options)
|
217
|
+
return values_at(*keys, **options) unless block_given?
|
218
|
+
hash = Hash[slice(*keys, **options)]
|
219
|
+
keys.map do |key|
|
220
|
+
if hash.key?(key)
|
221
|
+
hash[key]
|
222
|
+
else
|
223
|
+
yield key
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# (see Proxy#merge!)
|
229
|
+
def merge!(pairs, options = {})
|
230
|
+
with_connection do |conn|
|
231
|
+
conn.transaction do
|
232
|
+
existing = Hash[slice(*pairs.map { |k, _| k }, lock: true, **options)]
|
233
|
+
update_pairs, insert_pairs = pairs.partition { |k, _| existing.key?(k) }
|
234
|
+
insert_pairs.each { |key, value| conn_ins(conn, key, encode(conn, value)) }
|
235
|
+
|
236
|
+
if block_given?
|
237
|
+
update_pairs.map! do |key, new_value|
|
238
|
+
[key, yield(key, existing[key], new_value)]
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
update_pairs.each { |key, value| conn_upd(conn, key, encode(conn, value)) }
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
self
|
247
|
+
end
|
248
|
+
|
249
|
+
private
|
250
|
+
|
251
|
+
def connection_pool
|
252
|
+
self.class.retrieve_or_establish_connection_pool(@spec)
|
253
|
+
end
|
254
|
+
|
255
|
+
def default_create_table(table_name)
|
256
|
+
with_connection do |conn|
|
257
|
+
return if conn.table_exists?(table_name)
|
258
|
+
|
259
|
+
# Prevent multiple connections from attempting to create the table simultaneously.
|
260
|
+
self.class.connection_lock.synchronize do
|
261
|
+
conn.create_table(table_name, id: false) do |t|
|
262
|
+
# Do not use binary key (Issue #17)
|
263
|
+
t.string config.key_column, null: false
|
264
|
+
t.binary config.value_column
|
265
|
+
end
|
266
|
+
conn.add_index(table_name, config.key_column, unique: true)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
def arel_del
|
272
|
+
::Arel::DeleteManager.new.from(table)
|
273
|
+
end
|
274
|
+
|
275
|
+
def arel_sel
|
276
|
+
::Arel::SelectManager.new.from(table)
|
277
|
+
end
|
278
|
+
|
279
|
+
def arel_upd
|
280
|
+
::Arel::UpdateManager.new.table(table)
|
281
|
+
end
|
282
|
+
|
283
|
+
def arel_sel_key(key)
|
284
|
+
arel_sel.where(table[config.key_column].eq(key))
|
285
|
+
end
|
286
|
+
|
287
|
+
def conn_ins(conn, key, value)
|
288
|
+
ins = ::Arel::InsertManager.new.into(table)
|
289
|
+
ins.insert([[table[config.key_column], key], [table[config.value_column], value]])
|
290
|
+
conn.insert ins
|
291
|
+
end
|
292
|
+
|
293
|
+
def conn_upd(conn, key, value)
|
294
|
+
conn.update arel_upd.where(table[config.key_column].eq(key)).set([[table[config.value_column], value]])
|
295
|
+
end
|
296
|
+
|
297
|
+
def conn_sel_value(conn, key)
|
298
|
+
decode(conn, conn.select_value(arel_sel_key(key).project(table[config.value_column])))
|
299
|
+
end
|
300
|
+
|
301
|
+
def encode(conn, value)
|
302
|
+
if value == nil
|
303
|
+
nil
|
304
|
+
elsif conn.respond_to?(:escape_bytea)
|
305
|
+
conn.escape_bytea(value)
|
306
|
+
elsif defined?(::ActiveRecord::ConnectionAdapters::SQLite3Adapter) &&
|
307
|
+
conn.is_a?(::ActiveRecord::ConnectionAdapters::SQLite3Adapter)
|
308
|
+
Arel::Nodes::SqlLiteral.new("X'#{value.unpack('H*').first}'")
|
309
|
+
else
|
310
|
+
value
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
def decode(conn, value)
|
315
|
+
if value == nil
|
316
|
+
nil
|
317
|
+
elsif defined?(::ActiveModel::Type::Binary::Data) &&
|
318
|
+
value.is_a?(::ActiveModel::Type::Binary::Data)
|
319
|
+
value.to_s
|
320
|
+
elsif conn.respond_to?(:unescape_bytea)
|
321
|
+
conn.unescape_bytea(value)
|
322
|
+
else
|
323
|
+
value
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
# Feed the connection info into ActiveRecord and get back a name to use
|
328
|
+
# for getting the connection pool
|
329
|
+
def spec_for_connection(connection)
|
330
|
+
case connection
|
331
|
+
when Symbol
|
332
|
+
connection
|
333
|
+
when Hash, String
|
334
|
+
# Normalize the connection specification to a hash
|
335
|
+
resolver = ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new \
|
336
|
+
'dummy' => connection
|
337
|
+
|
338
|
+
# Turn the config into a standardised hash, sans a couple of bits
|
339
|
+
hash = resolver.resolve(:dummy)
|
340
|
+
hash.delete('name')
|
341
|
+
hash.delete(:password) # For security
|
342
|
+
# Make a name unique to this config
|
343
|
+
name = 'moneta?' + URI.encode_www_form(hash.to_a.sort)
|
344
|
+
# Add into configurations unless its already there (initially done without locking for
|
345
|
+
# speed)
|
346
|
+
unless self.class.configurations.key? name
|
347
|
+
self.class.connection_lock.synchronize do
|
348
|
+
self.class.configurations[name] = connection \
|
349
|
+
unless self.class.configurations.key? name
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
name.to_sym
|
354
|
+
else
|
355
|
+
::ActiveRecord::Base.connection_pool.spec.name.to_s
|
356
|
+
end
|
172
357
|
end
|
173
358
|
end
|
174
359
|
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module Moneta
|
2
|
+
module Adapters
|
3
|
+
# ActiveSupport::Cache::Store adapter
|
4
|
+
# @api public
|
5
|
+
class ActiveSupportCache < Adapter
|
6
|
+
include ExpiresSupport
|
7
|
+
|
8
|
+
supports :increment
|
9
|
+
|
10
|
+
# @!method initialize(options = {})
|
11
|
+
# @param [Hash] options
|
12
|
+
# @option options [ActiveSupport::Cache::Store] :backend (Rails.cache) Cache store to use
|
13
|
+
# @option options [Numeric] :expires default expiration in seconds
|
14
|
+
backend { Rails.cache if defined?(Rails) }
|
15
|
+
|
16
|
+
# (see Proxy#key?)
|
17
|
+
def key?(key, options = {})
|
18
|
+
backend.exist?(key).tap do |exists|
|
19
|
+
if exists && (expires = expires_value(options, nil)) != nil
|
20
|
+
value = backend.read(key, options)
|
21
|
+
backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# (see Proxy#load)
|
27
|
+
def load(key, options = {})
|
28
|
+
expires = expires_value(options, nil)
|
29
|
+
value = backend.read(key, options)
|
30
|
+
if value and expires != nil
|
31
|
+
backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
|
32
|
+
end
|
33
|
+
value
|
34
|
+
end
|
35
|
+
|
36
|
+
# (see Proxy#store)
|
37
|
+
def store(key, value, options = {})
|
38
|
+
expires = expires_value(options)
|
39
|
+
backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
|
40
|
+
value
|
41
|
+
end
|
42
|
+
|
43
|
+
# (see Proxy#increment)
|
44
|
+
def increment(key, amount = 1, options = {})
|
45
|
+
expires = expires_value(options)
|
46
|
+
options.delete(:raw)
|
47
|
+
existing = Integer(backend.fetch(key, options.merge(raw: true)) { 0 })
|
48
|
+
if amount > 0
|
49
|
+
backend.increment(key, amount, options.merge(expires_in: expires ? expires.seconds : nil))
|
50
|
+
elsif amount < 0
|
51
|
+
backend.decrement(key, -amount, options.merge(expires_in: expires ? expires.seconds : nil))
|
52
|
+
else
|
53
|
+
existing
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# (see Proxy#delete)
|
58
|
+
def delete(key, options = {})
|
59
|
+
value = backend.read(key, options)
|
60
|
+
if value != nil
|
61
|
+
backend.delete(key, options)
|
62
|
+
options[:raw] ? value.to_s : value
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# (see Proxy#clear)
|
67
|
+
def clear(options = {})
|
68
|
+
backend.clear
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
# (see Proxy#slice)
|
73
|
+
def slice(*keys, **options)
|
74
|
+
hash = backend.read_multi(*keys)
|
75
|
+
if (expires = expires_value(options, nil)) != nil
|
76
|
+
hash.each do |key, value|
|
77
|
+
backend.write(key, value, options.merge(expires_in: expires ? expires.seconds : nil))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
if options[:raw]
|
81
|
+
hash.each do |key, value|
|
82
|
+
hash[key] = value.to_s if value != nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
hash
|
86
|
+
end
|
87
|
+
|
88
|
+
# (see Proxy#values_at)
|
89
|
+
def values_at(*keys, **options)
|
90
|
+
hash = slice(*keys, **options)
|
91
|
+
keys.map { |key| hash[key] }
|
92
|
+
end
|
93
|
+
|
94
|
+
# (see Proxy#merge!)
|
95
|
+
def merge!(pairs, options = {})
|
96
|
+
if block_given?
|
97
|
+
existing = slice(*pairs.map { |k, _| k }, **options)
|
98
|
+
pairs = pairs.map do |key, new_value|
|
99
|
+
if existing.key?(key)
|
100
|
+
new_value = yield(key, existing[key], new_value)
|
101
|
+
end
|
102
|
+
|
103
|
+
[key, new_value]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
hash = Hash === pairs ? pairs : Hash[pairs.to_a]
|
108
|
+
expires = expires_value(options)
|
109
|
+
backend.write_multi(hash, options.merge(expires_in: expires ? expires.seconds : nil))
|
110
|
+
self
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def expires_value(options, default = config.expires)
|
116
|
+
super.tap { options.delete(:expires) unless options.frozen? }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|