moneta 1.0.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (488) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ruby.yml +425 -0
  3. data/.gitignore +3 -0
  4. data/.rspec +4 -0
  5. data/.rubocop.yml +194 -0
  6. data/.yardopts +6 -1
  7. data/CHANGES +88 -0
  8. data/CONTRIBUTORS +10 -2
  9. data/Gemfile +198 -76
  10. data/LICENSE +1 -1
  11. data/README.md +181 -83
  12. data/SPEC.md +70 -0
  13. data/feature_matrix.yaml +227 -0
  14. data/lib/action_dispatch/middleware/session/moneta_store.rb +1 -0
  15. data/lib/active_support/cache/moneta_store.rb +102 -6
  16. data/lib/moneta/adapter.rb +52 -0
  17. data/lib/moneta/adapters/activerecord.rb +284 -99
  18. data/lib/moneta/adapters/activesupportcache.rb +120 -0
  19. data/lib/moneta/adapters/cassandra.rb +303 -51
  20. data/lib/moneta/adapters/client.rb +72 -23
  21. data/lib/moneta/adapters/couch.rb +353 -49
  22. data/lib/moneta/adapters/datamapper.rb +11 -5
  23. data/lib/moneta/adapters/daybreak.rb +31 -20
  24. data/lib/moneta/adapters/dbm.rb +10 -16
  25. data/lib/moneta/adapters/file.rb +34 -13
  26. data/lib/moneta/adapters/fog.rb +5 -6
  27. data/lib/moneta/adapters/gdbm.rb +10 -16
  28. data/lib/moneta/adapters/hbase.rb +11 -13
  29. data/lib/moneta/adapters/kyotocabinet.rb +78 -17
  30. data/lib/moneta/adapters/leveldb.rb +35 -15
  31. data/lib/moneta/adapters/lmdb.rb +47 -23
  32. data/lib/moneta/adapters/localmemcache.rb +7 -13
  33. data/lib/moneta/adapters/lruhash.rb +45 -48
  34. data/lib/moneta/adapters/memcached/dalli.rb +67 -30
  35. data/lib/moneta/adapters/memcached/native.rb +33 -30
  36. data/lib/moneta/adapters/memcached.rb +1 -0
  37. data/lib/moneta/adapters/memory.rb +7 -7
  38. data/lib/moneta/adapters/mongo.rb +265 -6
  39. data/lib/moneta/adapters/null.rb +1 -2
  40. data/lib/moneta/adapters/pstore.rb +68 -25
  41. data/lib/moneta/adapters/redis.rb +101 -32
  42. data/lib/moneta/adapters/restclient.rb +17 -16
  43. data/lib/moneta/adapters/riak.rb +10 -11
  44. data/lib/moneta/adapters/sdbm.rb +10 -16
  45. data/lib/moneta/adapters/sequel/mysql.rb +66 -0
  46. data/lib/moneta/adapters/sequel/postgres.rb +80 -0
  47. data/lib/moneta/adapters/sequel/postgres_hstore.rb +240 -0
  48. data/lib/moneta/adapters/sequel/sqlite.rb +57 -0
  49. data/lib/moneta/adapters/sequel.rb +253 -52
  50. data/lib/moneta/adapters/sqlite.rb +103 -23
  51. data/lib/moneta/adapters/tdb.rb +21 -12
  52. data/lib/moneta/adapters/tokyocabinet.rb +24 -16
  53. data/lib/moneta/adapters/tokyotyrant.rb +49 -28
  54. data/lib/moneta/adapters/yaml.rb +1 -5
  55. data/lib/moneta/builder.rb +2 -3
  56. data/lib/moneta/cache.rb +11 -1
  57. data/lib/moneta/config.rb +101 -0
  58. data/lib/moneta/create_support.rb +21 -0
  59. data/lib/moneta/dbm_adapter.rb +31 -0
  60. data/lib/moneta/defaults.rb +320 -0
  61. data/lib/moneta/each_key_support.rb +27 -0
  62. data/lib/moneta/enumerable.rb +38 -0
  63. data/lib/moneta/expires.rb +112 -11
  64. data/lib/moneta/expires_support.rb +59 -0
  65. data/lib/moneta/fallback.rb +84 -0
  66. data/lib/moneta/hash_adapter.rb +68 -0
  67. data/lib/moneta/increment_support.rb +16 -0
  68. data/lib/moneta/lock.rb +26 -2
  69. data/lib/moneta/logger.rb +2 -2
  70. data/lib/moneta/nil_values.rb +35 -0
  71. data/lib/moneta/option_support.rb +51 -0
  72. data/lib/moneta/optionmerger.rb +0 -1
  73. data/lib/moneta/pool.rb +339 -28
  74. data/lib/moneta/proxy.rb +75 -2
  75. data/lib/moneta/server.rb +230 -68
  76. data/lib/moneta/shared.rb +36 -14
  77. data/lib/moneta/stack.rb +7 -7
  78. data/lib/moneta/synchronize.rb +5 -11
  79. data/lib/moneta/transformer/config.rb +64 -42
  80. data/lib/moneta/transformer/helper/bson.rb +7 -14
  81. data/lib/moneta/transformer/helper.rb +5 -5
  82. data/lib/moneta/transformer.rb +217 -23
  83. data/lib/moneta/utils.rb +3 -9
  84. data/lib/moneta/version.rb +1 -1
  85. data/lib/moneta/weak_each_key.rb +72 -0
  86. data/lib/moneta/wrapper.rb +30 -0
  87. data/lib/moneta.rb +29 -10
  88. data/lib/rack/cache/moneta.rb +13 -11
  89. data/lib/rack/moneta_rest.rb +2 -2
  90. data/lib/rack/session/moneta.rb +3 -4
  91. data/moneta.gemspec +19 -4
  92. data/script/benchmarks +311 -100
  93. data/script/contributors +11 -6
  94. data/script/parallel-tests +58 -83
  95. data/script/start-couchdb +27 -0
  96. data/script/start-hbase +47 -0
  97. data/script/start-services +5 -6
  98. data/script/update-feature-matrix +148 -0
  99. data/spec/active_support/cache_moneta_store_spec.rb +129 -106
  100. data/spec/features/concurrent_create.rb +42 -0
  101. data/spec/features/concurrent_increment.rb +39 -0
  102. data/spec/features/create.rb +25 -0
  103. data/spec/features/create_expires.rb +19 -0
  104. data/spec/features/default_expires.rb +14 -0
  105. data/spec/features/each_key.rb +119 -0
  106. data/spec/features/expires.rb +296 -0
  107. data/spec/features/features.rb +16 -0
  108. data/spec/features/increment.rb +98 -0
  109. data/spec/features/marshallable_key.rb +43 -0
  110. data/spec/features/marshallable_value.rb +7 -0
  111. data/spec/features/multiprocess.rb +8 -0
  112. data/spec/features/not_create.rb +7 -0
  113. data/spec/features/not_each_key.rb +7 -0
  114. data/spec/features/not_increment.rb +13 -0
  115. data/spec/features/not_persist.rb +9 -0
  116. data/spec/features/null.rb +66 -0
  117. data/spec/features/persist.rb +14 -0
  118. data/spec/features/returndifferent.rb +9 -0
  119. data/spec/features/returnsame.rb +9 -0
  120. data/spec/features/store.rb +261 -0
  121. data/spec/features/store_large.rb +13 -0
  122. data/spec/features/transform_value.rb +44 -0
  123. data/spec/features/transform_value_expires.rb +41 -0
  124. data/spec/helper.rb +345 -78
  125. data/spec/moneta/adapters/activerecord/adapter_activerecord_existing_connection_spec.rb +61 -0
  126. data/spec/moneta/adapters/activerecord/adapter_activerecord_spec.rb +126 -0
  127. data/spec/moneta/adapters/activerecord/standard_activerecord_spec.rb +21 -0
  128. data/spec/moneta/adapters/activerecord/standard_activerecord_with_expires_spec.rb +27 -0
  129. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +54 -0
  130. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +54 -0
  131. data/spec/moneta/adapters/activesupportcache/standard_activesupportcache_spec.rb +14 -0
  132. data/spec/moneta/adapters/cassandra/adapter_cassandra_spec.rb +17 -0
  133. data/spec/moneta/adapters/cassandra/adapter_cassandra_with_default_expires_spec.rb +18 -0
  134. data/spec/moneta/adapters/cassandra/helper.rb +18 -0
  135. data/spec/moneta/adapters/cassandra/standard_cassandra_spec.rb +18 -0
  136. data/spec/moneta/adapters/client/adapter_client_spec.rb +11 -0
  137. data/spec/moneta/adapters/client/client_helper.rb +25 -0
  138. data/spec/moneta/adapters/client/standard_client_tcp_spec.rb +23 -0
  139. data/spec/moneta/adapters/client/standard_client_unix_spec.rb +28 -0
  140. data/spec/moneta/adapters/cookie/adapter_cookie_spec.rb +7 -0
  141. data/spec/moneta/adapters/couch/adapter_couch_spec.rb +204 -0
  142. data/spec/moneta/adapters/couch/standard_couch_spec.rb +15 -0
  143. data/spec/moneta/adapters/couch/standard_couch_with_expires_spec.rb +19 -0
  144. data/spec/moneta/adapters/datamapper/adapter_datamapper_spec.rb +60 -0
  145. data/spec/moneta/adapters/datamapper/standard_datamapper_spec.rb +21 -0
  146. data/spec/moneta/adapters/datamapper/standard_datamapper_with_expires_spec.rb +26 -0
  147. data/spec/moneta/adapters/datamapper/standard_datamapper_with_repository_spec.rb +22 -0
  148. data/spec/moneta/adapters/daybreak/adapter_daybreak_spec.rb +7 -0
  149. data/spec/moneta/adapters/daybreak/standard_daybreak_spec.rb +7 -0
  150. data/spec/moneta/adapters/daybreak/standard_daybreak_with_expires_spec.rb +11 -0
  151. data/spec/moneta/adapters/dbm/adapter_dbm_spec.rb +7 -0
  152. data/spec/moneta/adapters/dbm/standard_dbm_spec.rb +7 -0
  153. data/spec/moneta/adapters/dbm/standard_dbm_with_expires_spec.rb +12 -0
  154. data/spec/moneta/adapters/faraday_helper.rb +10 -0
  155. data/spec/moneta/adapters/file/adapter_file_spec.rb +7 -0
  156. data/spec/moneta/adapters/file/standard_file_spec.rb +7 -0
  157. data/spec/moneta/adapters/file/standard_file_with_expires_spec.rb +11 -0
  158. data/spec/moneta/adapters/fog/adapter_fog_spec.rb +16 -0
  159. data/spec/moneta/adapters/fog/standard_fog_spec.rb +15 -0
  160. data/spec/moneta/adapters/fog/standard_fog_with_expires_spec.rb +20 -0
  161. data/spec/moneta/adapters/gdbm/adapter_gdbm_spec.rb +7 -0
  162. data/spec/moneta/adapters/gdbm/standard_gdbm_spec.rb +7 -0
  163. data/spec/moneta/adapters/gdbm/standard_gdbm_with_expires_spec.rb +11 -0
  164. data/spec/moneta/adapters/hashfile/standard_hashfile_spec.rb +7 -0
  165. data/spec/moneta/adapters/hashfile/standard_hashfile_with_expires_spec.rb +12 -0
  166. data/spec/moneta/adapters/hbase/adapter_hbase_spec.rb +7 -0
  167. data/spec/moneta/adapters/hbase/standard_hbase_spec.rb +4 -0
  168. data/spec/moneta/adapters/hbase/standard_hbase_with_expires_spec.rb +8 -0
  169. data/spec/moneta/adapters/kyotocabinet/adapter_kyotocabinet_spec.rb +7 -0
  170. data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_spec.rb +7 -0
  171. data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_with_expires_spec.rb +14 -0
  172. data/spec/moneta/adapters/leveldb/adapter_leveldb_spec.rb +7 -0
  173. data/spec/moneta/adapters/leveldb/standard_leveldb_spec.rb +7 -0
  174. data/spec/moneta/adapters/leveldb/standard_leveldb_with_expires_spec.rb +11 -0
  175. data/spec/moneta/adapters/lmdb/adapter_lmdb_spec.rb +7 -0
  176. data/spec/moneta/adapters/lmdb/adapter_lmdb_with_db_spec.rb +7 -0
  177. data/spec/moneta/adapters/lmdb/standard_lmdb_spec.rb +7 -0
  178. data/spec/moneta/adapters/lmdb/standard_lmdb_with_expires_spec.rb +11 -0
  179. data/spec/moneta/adapters/localmemcache/adapter_localmemcache_spec.rb +7 -0
  180. data/spec/moneta/adapters/localmemcache/standard_localmemcache_spec.rb +7 -0
  181. data/spec/moneta/adapters/localmemcache/standard_localmemcache_with_expires_spec.rb +11 -0
  182. data/spec/moneta/{adapter_lruhash_spec.rb → adapters/lruhash/adapter_lruhash_spec.rb} +15 -33
  183. data/spec/moneta/adapters/lruhash/standard_lruhash_spec.rb +4 -0
  184. data/spec/moneta/adapters/lruhash/standard_lruhash_with_expires_spec.rb +8 -0
  185. data/spec/moneta/adapters/memcached/adapter_memcached_spec.rb +13 -0
  186. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +25 -0
  187. data/spec/moneta/adapters/memcached/dalli/standard_memcached_dalli_spec.rb +11 -0
  188. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +25 -0
  189. data/spec/moneta/adapters/memcached/native/standard_memcached_native_spec.rb +11 -0
  190. data/spec/moneta/adapters/memcached/standard_memcached_spec.rb +17 -0
  191. data/spec/moneta/adapters/memcached_helper.rb +20 -0
  192. data/spec/moneta/adapters/memory/adapter_memory_spec.rb +7 -0
  193. data/spec/moneta/adapters/memory/standard_memory_spec.rb +4 -0
  194. data/spec/moneta/adapters/memory/standard_memory_with_compress_spec.rb +9 -0
  195. data/spec/moneta/adapters/memory/standard_memory_with_expires_spec.rb +8 -0
  196. data/spec/moneta/adapters/memory/standard_memory_with_json_key_serializer_spec.rb +4 -0
  197. data/spec/moneta/adapters/memory/standard_memory_with_json_serializer_spec.rb +9 -0
  198. data/spec/moneta/adapters/memory/standard_memory_with_json_value_serializer_spec.rb +9 -0
  199. data/spec/moneta/adapters/memory/standard_memory_with_prefix_spec.rb +41 -0
  200. data/spec/moneta/adapters/memory/standard_memory_with_snappy_compress_spec.rb +9 -0
  201. data/spec/moneta/adapters/mongo/adapter_mongo_spec.rb +41 -0
  202. data/spec/moneta/adapters/mongo/adapter_mongo_with_default_expires_spec.rb +14 -0
  203. data/spec/moneta/adapters/mongo/standard_mongo_spec.rb +7 -0
  204. data/spec/moneta/adapters/null/null_adapter_spec.rb +7 -0
  205. data/spec/moneta/adapters/null/standard_null_spec.rb +4 -0
  206. data/spec/moneta/adapters/pstore/adapter_pstore_spec.rb +7 -0
  207. data/spec/moneta/adapters/pstore/standard_pstore_spec.rb +9 -0
  208. data/spec/moneta/adapters/pstore/standard_pstore_with_expires_spec.rb +13 -0
  209. data/spec/moneta/adapters/redis/adapter_redis_spec.rb +20 -0
  210. data/spec/moneta/adapters/redis/standard_redis_spec.rb +14 -0
  211. data/spec/moneta/adapters/restclient/adapter_restclient_spec.rb +13 -0
  212. data/spec/moneta/adapters/restclient/helper.rb +12 -0
  213. data/spec/moneta/adapters/restclient/standard_restclient_spec.rb +13 -0
  214. data/spec/moneta/adapters/riak/adapter_riak_spec.rb +14 -0
  215. data/spec/moneta/adapters/riak/standard_riak_spec.rb +10 -0
  216. data/spec/moneta/adapters/riak/standard_riak_with_expires_spec.rb +14 -0
  217. data/spec/moneta/adapters/sdbm/adapter_sdbm_spec.rb +7 -0
  218. data/spec/moneta/adapters/sdbm/standard_sdbm_spec.rb +7 -0
  219. data/spec/moneta/adapters/sdbm/standard_sdbm_with_expires_spec.rb +11 -0
  220. data/spec/moneta/adapters/sequel/adapter_sequel_spec.rb +133 -0
  221. data/spec/moneta/adapters/sequel/helper.rb +80 -0
  222. data/spec/moneta/adapters/sequel/standard_sequel_spec.rb +8 -0
  223. data/spec/moneta/adapters/sequel/standard_sequel_with_expires_spec.rb +18 -0
  224. data/spec/moneta/adapters/sqlite/adapter_sqlite_spec.rb +7 -0
  225. data/spec/moneta/adapters/sqlite/standard_sqlite_spec.rb +7 -0
  226. data/spec/moneta/adapters/sqlite/standard_sqlite_with_expires_spec.rb +15 -0
  227. data/spec/moneta/adapters/tdb/adapter_tdb_spec.rb +7 -0
  228. data/spec/moneta/adapters/tdb/standard_tdb_spec.rb +7 -0
  229. data/spec/moneta/adapters/tdb/standard_tdb_with_expires_spec.rb +11 -0
  230. data/spec/moneta/adapters/tokyocabinet/adapter_tokyocabinet_bdb_spec.rb +7 -0
  231. data/spec/moneta/adapters/tokyocabinet/adapter_tokyocabinet_hdb_spec.rb +7 -0
  232. data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_spec.rb +7 -0
  233. data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_with_expires_spec.rb +11 -0
  234. data/spec/moneta/adapters/tokyotyrant/adapter_tokyotyrant_spec.rb +11 -0
  235. data/spec/moneta/adapters/tokyotyrant/helper.rb +12 -0
  236. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_spec.rb +7 -0
  237. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_with_expires_spec.rb +12 -0
  238. data/spec/moneta/adapters/yaml/adapter_yaml_spec.rb +7 -0
  239. data/spec/moneta/adapters/yaml/standard_yaml_spec.rb +9 -0
  240. data/spec/moneta/adapters/yaml/standard_yaml_with_expires_spec.rb +13 -0
  241. data/spec/moneta/builder_spec.rb +22 -1
  242. data/spec/moneta/config_spec.rb +219 -0
  243. data/spec/moneta/mutex_spec.rb +6 -23
  244. data/spec/moneta/proxies/cache/cache_file_memory_spec.rb +24 -0
  245. data/spec/moneta/proxies/cache/cache_memory_null_spec.rb +12 -0
  246. data/spec/moneta/proxies/enumerable/enumerable_spec.rb +26 -0
  247. data/spec/moneta/proxies/expires/expires_file_spec.rb +28 -0
  248. data/spec/moneta/proxies/expires/expires_memory_spec.rb +15 -0
  249. data/spec/moneta/proxies/expires/expires_memory_with_default_expires_spec.rb +16 -0
  250. data/spec/moneta/proxies/fallback/fallback_spec.rb +42 -0
  251. data/spec/moneta/proxies/lock/lock_spec.rb +10 -0
  252. data/spec/moneta/{optionmerger_spec.rb → proxies/optionmerger/optionmerger_spec.rb} +2 -18
  253. data/spec/moneta/proxies/pool/pool_spec.rb +353 -0
  254. data/spec/moneta/proxies/proxy/proxy_expires_memory_spec.rb +16 -0
  255. data/spec/moneta/proxies/shared/shared_tcp_spec.rb +59 -0
  256. data/spec/moneta/proxies/shared/shared_unix_spec.rb +58 -0
  257. data/spec/moneta/proxies/transformer/transformer_bencode_spec.rb +19 -0
  258. data/spec/moneta/proxies/transformer/transformer_bert_spec.rb +19 -0
  259. data/spec/moneta/proxies/transformer/transformer_bson_spec.rb +25 -0
  260. data/spec/moneta/proxies/transformer/transformer_bzip2_spec.rb +19 -0
  261. data/spec/moneta/proxies/transformer/transformer_json_spec.rb +19 -0
  262. data/spec/moneta/proxies/transformer/transformer_key_inspect_spec.rb +17 -0
  263. data/spec/moneta/proxies/transformer/transformer_key_marshal_spec.rb +17 -0
  264. data/spec/moneta/proxies/transformer/transformer_key_to_s_spec.rb +17 -0
  265. data/spec/moneta/proxies/transformer/transformer_key_yaml_spec.rb +17 -0
  266. data/spec/moneta/proxies/transformer/transformer_lz4_spec.rb +19 -0
  267. data/spec/moneta/proxies/transformer/transformer_lzma_spec.rb +19 -0
  268. data/spec/moneta/proxies/transformer/transformer_lzo_spec.rb +19 -0
  269. data/spec/moneta/proxies/transformer/transformer_marshal_base64_spec.rb +20 -0
  270. data/spec/moneta/proxies/transformer/transformer_marshal_city128_spec.rb +15 -0
  271. data/spec/moneta/proxies/transformer/transformer_marshal_city32_spec.rb +15 -0
  272. data/spec/moneta/proxies/transformer/transformer_marshal_city64_spec.rb +15 -0
  273. data/spec/moneta/proxies/transformer/transformer_marshal_escape_spec.rb +21 -0
  274. data/spec/moneta/proxies/transformer/transformer_marshal_hex_spec.rb +20 -0
  275. data/spec/moneta/proxies/transformer/transformer_marshal_hmac_spec.rb +19 -0
  276. data/spec/moneta/proxies/transformer/transformer_marshal_md5_spec.rb +15 -0
  277. data/spec/moneta/proxies/transformer/transformer_marshal_md5_spread_spec.rb +15 -0
  278. data/spec/moneta/proxies/transformer/transformer_marshal_prefix_base64_spec.rb +33 -0
  279. data/spec/moneta/proxies/transformer/transformer_marshal_prefix_spec.rb +15 -0
  280. data/spec/moneta/proxies/transformer/transformer_marshal_qp_spec.rb +20 -0
  281. data/spec/moneta/proxies/transformer/transformer_marshal_rmd160_spec.rb +15 -0
  282. data/spec/moneta/proxies/transformer/transformer_marshal_sha1_spec.rb +15 -0
  283. data/spec/moneta/proxies/transformer/transformer_marshal_sha256_spec.rb +15 -0
  284. data/spec/moneta/proxies/transformer/transformer_marshal_sha384_spec.rb +15 -0
  285. data/spec/moneta/proxies/transformer/transformer_marshal_sha512_spec.rb +15 -0
  286. data/spec/moneta/proxies/transformer/transformer_marshal_spec.rb +19 -0
  287. data/spec/moneta/proxies/transformer/transformer_marshal_truncate_spec.rb +15 -0
  288. data/spec/moneta/proxies/transformer/transformer_marshal_urlsafe_base64_spec.rb +20 -0
  289. data/spec/moneta/proxies/transformer/transformer_marshal_uuencode_spec.rb +19 -0
  290. data/spec/moneta/proxies/transformer/transformer_msgpack_spec.rb +19 -0
  291. data/spec/moneta/proxies/transformer/transformer_ox_spec.rb +19 -0
  292. data/spec/moneta/proxies/transformer/transformer_php_spec.rb +19 -0
  293. data/spec/moneta/proxies/transformer/transformer_quicklz_spec.rb +19 -0
  294. data/spec/moneta/proxies/transformer/transformer_snappy_spec.rb +19 -0
  295. data/spec/moneta/proxies/transformer/transformer_tnet_spec.rb +19 -0
  296. data/spec/moneta/proxies/transformer/transformer_value_marshal_spec.rb +19 -0
  297. data/spec/moneta/proxies/transformer/transformer_value_yaml_spec.rb +19 -0
  298. data/spec/moneta/proxies/transformer/transformer_yaml_spec.rb +19 -0
  299. data/spec/moneta/proxies/transformer/transformer_zlib_spec.rb +19 -0
  300. data/spec/moneta/proxies/weak_create/weak_create_spec.rb +21 -0
  301. data/spec/moneta/proxies/weak_each_key/weak_each_key_spec.rb +22 -0
  302. data/spec/moneta/proxies/weak_increment/weak_increment_spec.rb +21 -0
  303. data/spec/moneta/semaphore_spec.rb +6 -23
  304. data/spec/moneta/stack_file_memory_spec.rb +5 -29
  305. data/spec/moneta/stack_memory_file_spec.rb +5 -31
  306. data/spec/rack/session_moneta_spec.rb +44 -25
  307. data/spec/restserver.rb +44 -0
  308. data/{spec → test}/action_dispatch/fixtures/session_autoload_test/foo.rb +0 -0
  309. data/{spec/action_dispatch/session_moneta_store_spec.rb → test/action_dispatch/session_moneta_store_test.rb} +13 -5
  310. metadata +545 -369
  311. data/.travis.yml +0 -55
  312. data/lib/moneta/adapters/mongo/base.rb +0 -78
  313. data/lib/moneta/adapters/mongo/moped.rb +0 -107
  314. data/lib/moneta/adapters/mongo/official.rb +0 -117
  315. data/lib/moneta/mixins.rb +0 -343
  316. data/script/generate-specs +0 -2696
  317. data/script/install-kyotocabinet +0 -17
  318. data/spec/moneta/adapter_activerecord_exisiting_connection_spec.rb +0 -36
  319. data/spec/moneta/adapter_activerecord_spec.rb +0 -65
  320. data/spec/moneta/adapter_cassandra_spec.rb +0 -33
  321. data/spec/moneta/adapter_cassandra_with_default_expires_spec.rb +0 -34
  322. data/spec/moneta/adapter_client_spec.rb +0 -35
  323. data/spec/moneta/adapter_cookie_spec.rb +0 -30
  324. data/spec/moneta/adapter_couch_spec.rb +0 -36
  325. data/spec/moneta/adapter_datamapper_spec.rb +0 -64
  326. data/spec/moneta/adapter_daybreak_spec.rb +0 -31
  327. data/spec/moneta/adapter_dbm_spec.rb +0 -31
  328. data/spec/moneta/adapter_file_spec.rb +0 -34
  329. data/spec/moneta/adapter_fog_spec.rb +0 -37
  330. data/spec/moneta/adapter_gdbm_spec.rb +0 -31
  331. data/spec/moneta/adapter_hbase_spec.rb +0 -33
  332. data/spec/moneta/adapter_kyotocabinet_spec.rb +0 -31
  333. data/spec/moneta/adapter_leveldb_spec.rb +0 -31
  334. data/spec/moneta/adapter_lmdb_spec.rb +0 -32
  335. data/spec/moneta/adapter_lmdb_with_db_spec.rb +0 -32
  336. data/spec/moneta/adapter_localmemcache_spec.rb +0 -32
  337. data/spec/moneta/adapter_memcached_dalli_spec.rb +0 -36
  338. data/spec/moneta/adapter_memcached_dalli_with_default_expires_spec.rb +0 -37
  339. data/spec/moneta/adapter_memcached_native_spec.rb +0 -36
  340. data/spec/moneta/adapter_memcached_native_with_default_expires_spec.rb +0 -37
  341. data/spec/moneta/adapter_memcached_spec.rb +0 -36
  342. data/spec/moneta/adapter_memcached_with_default_expires_spec.rb +0 -37
  343. data/spec/moneta/adapter_memory_spec.rb +0 -184
  344. data/spec/moneta/adapter_mongo_moped_spec.rb +0 -64
  345. data/spec/moneta/adapter_mongo_moped_with_default_expires_spec.rb +0 -53
  346. data/spec/moneta/adapter_mongo_official_spec.rb +0 -64
  347. data/spec/moneta/adapter_mongo_official_with_default_expires_spec.rb +0 -53
  348. data/spec/moneta/adapter_mongo_spec.rb +0 -51
  349. data/spec/moneta/adapter_mongo_with_default_expires_spec.rb +0 -53
  350. data/spec/moneta/adapter_pstore_spec.rb +0 -250
  351. data/spec/moneta/adapter_redis_spec.rb +0 -36
  352. data/spec/moneta/adapter_redis_with_default_expires_spec.rb +0 -37
  353. data/spec/moneta/adapter_restclient_spec.rb +0 -33
  354. data/spec/moneta/adapter_riak_spec.rb +0 -36
  355. data/spec/moneta/adapter_sdbm_spec.rb +0 -30
  356. data/spec/moneta/adapter_sequel_spec.rb +0 -34
  357. data/spec/moneta/adapter_sqlite_spec.rb +0 -32
  358. data/spec/moneta/adapter_tdb_spec.rb +0 -31
  359. data/spec/moneta/adapter_tokyocabinet_bdb_spec.rb +0 -31
  360. data/spec/moneta/adapter_tokyocabinet_hdb_spec.rb +0 -31
  361. data/spec/moneta/adapter_tokyotyrant_spec.rb +0 -34
  362. data/spec/moneta/adapter_yaml_spec.rb +0 -57
  363. data/spec/moneta/cache_file_memory_spec.rb +0 -50
  364. data/spec/moneta/cache_memory_null_spec.rb +0 -35
  365. data/spec/moneta/expires_file_spec.rb +0 -78
  366. data/spec/moneta/expires_memory_spec.rb +0 -189
  367. data/spec/moneta/expires_memory_with_default_expires_spec.rb +0 -190
  368. data/spec/moneta/lock_spec.rb +0 -187
  369. data/spec/moneta/null_adapter_spec.rb +0 -86
  370. data/spec/moneta/pool_spec.rb +0 -38
  371. data/spec/moneta/proxy_expires_memory_spec.rb +0 -191
  372. data/spec/moneta/proxy_redis_spec.rb +0 -40
  373. data/spec/moneta/shared_tcp_spec.rb +0 -46
  374. data/spec/moneta/shared_unix_spec.rb +0 -46
  375. data/spec/moneta/standard_activerecord_spec.rb +0 -253
  376. data/spec/moneta/standard_activerecord_with_expires_spec.rb +0 -255
  377. data/spec/moneta/standard_cassandra_spec.rb +0 -252
  378. data/spec/moneta/standard_client_tcp_spec.rb +0 -269
  379. data/spec/moneta/standard_client_unix_spec.rb +0 -254
  380. data/spec/moneta/standard_couch_spec.rb +0 -252
  381. data/spec/moneta/standard_couch_with_expires_spec.rb +0 -254
  382. data/spec/moneta/standard_datamapper_spec.rb +0 -254
  383. data/spec/moneta/standard_datamapper_with_expires_spec.rb +0 -256
  384. data/spec/moneta/standard_datamapper_with_repository_spec.rb +0 -254
  385. data/spec/moneta/standard_daybreak_spec.rb +0 -250
  386. data/spec/moneta/standard_daybreak_with_expires_spec.rb +0 -252
  387. data/spec/moneta/standard_dbm_spec.rb +0 -250
  388. data/spec/moneta/standard_dbm_with_expires_spec.rb +0 -252
  389. data/spec/moneta/standard_file_spec.rb +0 -253
  390. data/spec/moneta/standard_file_with_expires_spec.rb +0 -255
  391. data/spec/moneta/standard_fog_spec.rb +0 -256
  392. data/spec/moneta/standard_fog_with_expires_spec.rb +0 -258
  393. data/spec/moneta/standard_gdbm_spec.rb +0 -250
  394. data/spec/moneta/standard_gdbm_with_expires_spec.rb +0 -252
  395. data/spec/moneta/standard_hashfile_spec.rb +0 -253
  396. data/spec/moneta/standard_hashfile_with_expires_spec.rb +0 -255
  397. data/spec/moneta/standard_hbase_spec.rb +0 -252
  398. data/spec/moneta/standard_hbase_with_expires_spec.rb +0 -255
  399. data/spec/moneta/standard_kyotocabinet_spec.rb +0 -250
  400. data/spec/moneta/standard_kyotocabinet_with_expires_spec.rb +0 -252
  401. data/spec/moneta/standard_leveldb_spec.rb +0 -250
  402. data/spec/moneta/standard_leveldb_with_expires_spec.rb +0 -252
  403. data/spec/moneta/standard_lmdb_spec.rb +0 -251
  404. data/spec/moneta/standard_lmdb_with_expires_spec.rb +0 -253
  405. data/spec/moneta/standard_localmemcache_spec.rb +0 -251
  406. data/spec/moneta/standard_localmemcache_with_expires_spec.rb +0 -252
  407. data/spec/moneta/standard_lruhash_spec.rb +0 -187
  408. data/spec/moneta/standard_lruhash_with_expires_spec.rb +0 -189
  409. data/spec/moneta/standard_memcached_dalli_spec.rb +0 -255
  410. data/spec/moneta/standard_memcached_native_spec.rb +0 -255
  411. data/spec/moneta/standard_memcached_spec.rb +0 -255
  412. data/spec/moneta/standard_memory_spec.rb +0 -187
  413. data/spec/moneta/standard_memory_with_compress_spec.rb +0 -187
  414. data/spec/moneta/standard_memory_with_expires_spec.rb +0 -189
  415. data/spec/moneta/standard_memory_with_json_key_serializer_spec.rb +0 -86
  416. data/spec/moneta/standard_memory_with_json_serializer_spec.rb +0 -49
  417. data/spec/moneta/standard_memory_with_json_value_serializer_spec.rb +0 -90
  418. data/spec/moneta/standard_memory_with_prefix_spec.rb +0 -187
  419. data/spec/moneta/standard_memory_with_snappy_compress_spec.rb +0 -187
  420. data/spec/moneta/standard_mongo_moped_spec.rb +0 -255
  421. data/spec/moneta/standard_mongo_official_spec.rb +0 -255
  422. data/spec/moneta/standard_mongo_spec.rb +0 -255
  423. data/spec/moneta/standard_null_spec.rb +0 -120
  424. data/spec/moneta/standard_pstore_spec.rb +0 -253
  425. data/spec/moneta/standard_pstore_with_expires_spec.rb +0 -255
  426. data/spec/moneta/standard_redis_spec.rb +0 -255
  427. data/spec/moneta/standard_restclient_spec.rb +0 -252
  428. data/spec/moneta/standard_riak_spec.rb +0 -255
  429. data/spec/moneta/standard_riak_with_expires_spec.rb +0 -256
  430. data/spec/moneta/standard_sdbm_spec.rb +0 -249
  431. data/spec/moneta/standard_sdbm_with_expires_spec.rb +0 -251
  432. data/spec/moneta/standard_sequel_spec.rb +0 -253
  433. data/spec/moneta/standard_sequel_with_expires_spec.rb +0 -255
  434. data/spec/moneta/standard_sqlite_spec.rb +0 -251
  435. data/spec/moneta/standard_sqlite_with_expires_spec.rb +0 -253
  436. data/spec/moneta/standard_tdb_spec.rb +0 -250
  437. data/spec/moneta/standard_tdb_with_expires_spec.rb +0 -252
  438. data/spec/moneta/standard_tokyocabinet_spec.rb +0 -250
  439. data/spec/moneta/standard_tokyocabinet_with_expires_spec.rb +0 -252
  440. data/spec/moneta/standard_tokyotyrant_spec.rb +0 -253
  441. data/spec/moneta/standard_tokyotyrant_with_expires_spec.rb +0 -255
  442. data/spec/moneta/standard_yaml_spec.rb +0 -250
  443. data/spec/moneta/standard_yaml_with_expires_spec.rb +0 -252
  444. data/spec/moneta/transformer_bencode_spec.rb +0 -55
  445. data/spec/moneta/transformer_bert_spec.rb +0 -55
  446. data/spec/moneta/transformer_bson_spec.rb +0 -60
  447. data/spec/moneta/transformer_bzip2_spec.rb +0 -55
  448. data/spec/moneta/transformer_json_spec.rb +0 -55
  449. data/spec/moneta/transformer_key_inspect_spec.rb +0 -91
  450. data/spec/moneta/transformer_key_marshal_spec.rb +0 -191
  451. data/spec/moneta/transformer_key_to_s_spec.rb +0 -91
  452. data/spec/moneta/transformer_key_yaml_spec.rb +0 -191
  453. data/spec/moneta/transformer_lz4_spec.rb +0 -55
  454. data/spec/moneta/transformer_lzma_spec.rb +0 -55
  455. data/spec/moneta/transformer_lzo_spec.rb +0 -55
  456. data/spec/moneta/transformer_marshal_base64_spec.rb +0 -194
  457. data/spec/moneta/transformer_marshal_city128_spec.rb +0 -194
  458. data/spec/moneta/transformer_marshal_city32_spec.rb +0 -194
  459. data/spec/moneta/transformer_marshal_city64_spec.rb +0 -194
  460. data/spec/moneta/transformer_marshal_escape_spec.rb +0 -194
  461. data/spec/moneta/transformer_marshal_hex_spec.rb +0 -194
  462. data/spec/moneta/transformer_marshal_hmac_spec.rb +0 -194
  463. data/spec/moneta/transformer_marshal_md5_spec.rb +0 -194
  464. data/spec/moneta/transformer_marshal_md5_spread_spec.rb +0 -194
  465. data/spec/moneta/transformer_marshal_prefix_spec.rb +0 -194
  466. data/spec/moneta/transformer_marshal_qp_spec.rb +0 -194
  467. data/spec/moneta/transformer_marshal_rmd160_spec.rb +0 -194
  468. data/spec/moneta/transformer_marshal_sha1_spec.rb +0 -194
  469. data/spec/moneta/transformer_marshal_sha256_spec.rb +0 -194
  470. data/spec/moneta/transformer_marshal_sha384_spec.rb +0 -194
  471. data/spec/moneta/transformer_marshal_sha512_spec.rb +0 -194
  472. data/spec/moneta/transformer_marshal_spec.rb +0 -191
  473. data/spec/moneta/transformer_marshal_truncate_spec.rb +0 -194
  474. data/spec/moneta/transformer_marshal_uuencode_spec.rb +0 -194
  475. data/spec/moneta/transformer_msgpack_spec.rb +0 -55
  476. data/spec/moneta/transformer_ox_spec.rb +0 -191
  477. data/spec/moneta/transformer_php_spec.rb +0 -55
  478. data/spec/moneta/transformer_quicklz_spec.rb +0 -55
  479. data/spec/moneta/transformer_snappy_spec.rb +0 -55
  480. data/spec/moneta/transformer_tnet_spec.rb +0 -55
  481. data/spec/moneta/transformer_value_marshal_spec.rb +0 -191
  482. data/spec/moneta/transformer_value_yaml_spec.rb +0 -191
  483. data/spec/moneta/transformer_yaml_spec.rb +0 -191
  484. data/spec/moneta/transformer_zlib_spec.rb +0 -55
  485. data/spec/moneta/weak_create_spec.rb +0 -41
  486. data/spec/moneta/weak_increment_spec.rb +0 -41
  487. data/spec/monetaspecs.rb +0 -51267
  488. data/spec/quality_spec.rb +0 -51
@@ -1,11 +1,270 @@
1
+ require 'mongo'
2
+
1
3
  module Moneta
2
4
  module Adapters
3
- begin
4
- require 'moneta/adapters/mongo/official'
5
- Mongo = MongoOfficial
6
- rescue LoadError
7
- require 'moneta/adapters/mongo/moped'
8
- Mongo = MongoMoped
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
@@ -6,8 +6,7 @@ module Moneta
6
6
  include Defaults
7
7
 
8
8
  # @param [Hash] options Options hash
9
- def initialize(options = {})
10
- end
9
+ def initialize(options = {}); end
11
10
 
12
11
  # (see Proxy#key?)
13
12
  def key?(key, options = {})
@@ -5,60 +5,72 @@ module Moneta
5
5
  module Adapters
6
6
  # PStore backend
7
7
  # @api public
8
- class PStore
9
- include Defaults
8
+ class PStore < Adapter
9
+ include NilValues
10
10
 
11
- supports :create, :increment
12
- attr_reader :backend
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
- @backend = options[:backend] ||
19
- begin
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
- @backend.transaction(true) { @backend.root?(key) }
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
- @backend.transaction(true) { @backend[key] }
44
+ transaction(true) { backend[key] }
34
45
  end
35
46
 
36
47
  # (see Proxy#store)
37
48
  def store(key, value, options = {})
38
- @backend.transaction { @backend[key] = value }
49
+ transaction { backend[key] = value }
39
50
  end
40
51
 
41
52
  # (see Proxy#delete)
42
53
  def delete(key, options = {})
43
- @backend.transaction { @backend.delete(key) }
54
+ transaction { backend.delete(key) }
44
55
  end
45
56
 
46
57
  # (see Proxy#increment)
47
58
  def increment(key, amount = 1, options = {})
48
- @backend.transaction do
49
- value = Utils.to_int(@backend[key]) + amount
50
- @backend[key] = value.to_s
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
- @backend.transaction do
58
- if @backend.root?(key)
69
+ transaction do
70
+ if backend.root?(key)
59
71
  false
60
72
  else
61
- @backend[key] = value
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
- @backend.transaction do
70
- @backend.roots.each do |key|
71
- @backend.delete(key)
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
- def new_store(options)
80
- ::PStore.new(options[:file], options[:threadsafe])
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
- # @param [Hash] options
15
- # @option options [Integer] :expires Default expiration time
16
- # @option options [::Redis] :backend Use existing backend instance
17
- # @option options Other options passed to `Redis#new`
18
- def initialize(options = {})
19
- self.default_expires = options.delete(:expires)
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
- if @backend.exists(key)
29
- update_expires(key, options, nil)
30
- true
31
- else
32
- false
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
- value = @backend.get(key)
39
- update_expires(key, options, nil)
40
- value
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
- @backend.setex(key, expires, value)
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
- if value = load(key, options)
56
- @backend.del(key)
57
- value
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
- value = @backend.incrby(key, amount)
64
- update_expires(key, options)
65
- value
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, options)
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(key, options, default = @default_expires)
93
- case expires = expires_value(options, default)
138
+ def update_expires(pipeline_handle, key, expires)
139
+ case expires
94
140
  when false
95
- @backend.persist(key)
141
+ pipeline_handle.persist(key)
96
142
  when Numeric
97
- @backend.expire(key, expires)
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
- include Defaults
9
-
10
- attr_reader :backend
11
-
12
- # @param [Hash] options
13
- # @option options [String] :url URL
14
- # @option options [Faraday connection] :backend Use existing backend instance
15
- def initialize(options = {})
16
- raise ArgumentError, 'Option :url is required' unless url = options[:url]
17
- @backend = options[:backend] || ::Faraday.new(url: url)
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
- @backend.head(key).status == 200
23
+ backend.head(key).status == 200
23
24
  end
24
25
 
25
26
  # (see Proxy#load)
26
27
  def load(key, options = {})
27
- response = @backend.get(key)
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 = @backend.post(key, value)
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 = @backend.delete(key)
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
- @backend.delete ''
47
+ backend.delete ''
47
48
  self
48
49
  end
49
50
  end