moneta 0.7.20 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -5
  3. data/CHANGES +6 -0
  4. data/CONTRIBUTORS +2 -0
  5. data/Gemfile +31 -44
  6. data/LICENSE +1 -1
  7. data/README.md +82 -59
  8. data/lib/active_support/cache/moneta_store.rb +3 -3
  9. data/lib/moneta.rb +4 -2
  10. data/lib/moneta/adapters/activerecord.rb +8 -8
  11. data/lib/moneta/adapters/cassandra.rb +8 -8
  12. data/lib/moneta/adapters/couch.rb +1 -1
  13. data/lib/moneta/adapters/datamapper.rb +5 -5
  14. data/lib/moneta/adapters/daybreak.rb +1 -1
  15. data/lib/moneta/adapters/fog.rb +2 -2
  16. data/lib/moneta/adapters/lmdb.rb +1 -1
  17. data/lib/moneta/adapters/localmemcache.rb +1 -1
  18. data/lib/moneta/adapters/memcached/dalli.rb +3 -3
  19. data/lib/moneta/adapters/memcached/native.rb +1 -1
  20. data/lib/moneta/adapters/mongo.rb +6 -164
  21. data/lib/moneta/adapters/mongo/base.rb +63 -0
  22. data/lib/moneta/adapters/mongo/moped.rb +113 -0
  23. data/lib/moneta/adapters/mongo/official.rb +121 -0
  24. data/lib/moneta/adapters/pstore.rb +1 -5
  25. data/lib/moneta/adapters/restclient.rb +1 -1
  26. data/lib/moneta/adapters/sequel.rb +10 -10
  27. data/lib/moneta/cache.rb +1 -1
  28. data/lib/moneta/logger.rb +2 -2
  29. data/lib/moneta/mixins.rb +4 -4
  30. data/lib/moneta/shared.rb +2 -2
  31. data/lib/moneta/stack.rb +2 -2
  32. data/lib/moneta/synchronize.rb +2 -2
  33. data/lib/moneta/transformer.rb +5 -5
  34. data/lib/moneta/transformer/config.rb +38 -40
  35. data/lib/moneta/version.rb +1 -1
  36. data/lib/rack/cache/moneta.rb +1 -1
  37. data/lib/rack/moneta_cookies.rb +2 -2
  38. data/lib/rack/moneta_rest.rb +1 -1
  39. data/lib/rack/moneta_store.rb +4 -4
  40. data/lib/rack/session/moneta.rb +14 -2
  41. data/moneta.gemspec +1 -0
  42. data/script/benchmarks +97 -96
  43. data/script/generate-specs +763 -730
  44. data/script/install-bundle +1 -1
  45. data/script/install-kyotocabinet +2 -2
  46. data/script/memusage +1 -1
  47. data/script/upload-bundle +1 -1
  48. data/spec/action_dispatch/session_moneta_store_spec.rb +5 -5
  49. data/spec/active_support/cache_moneta_store_spec.rb +20 -20
  50. data/spec/moneta/adapter_activerecord_exisiting_connection_spec.rb +2 -2
  51. data/spec/moneta/adapter_activerecord_spec.rb +6 -6
  52. data/spec/moneta/adapter_cassandra_spec.rb +1 -1
  53. data/spec/moneta/adapter_cassandra_with_default_expires_spec.rb +1 -1
  54. data/spec/moneta/adapter_couch_spec.rb +1 -1
  55. data/spec/moneta/adapter_datamapper_spec.rb +6 -6
  56. data/spec/moneta/adapter_daybreak_spec.rb +1 -1
  57. data/spec/moneta/adapter_dbm_spec.rb +1 -1
  58. data/spec/moneta/adapter_file_spec.rb +1 -1
  59. data/spec/moneta/adapter_fog_spec.rb +4 -4
  60. data/spec/moneta/adapter_gdbm_spec.rb +1 -1
  61. data/spec/moneta/adapter_hbase_spec.rb +1 -1
  62. data/spec/moneta/adapter_kyotocabinet_spec.rb +1 -1
  63. data/spec/moneta/adapter_leveldb_spec.rb +1 -1
  64. data/spec/moneta/adapter_lmdb_spec.rb +1 -1
  65. data/spec/moneta/adapter_lmdb_with_db_spec.rb +1 -1
  66. data/spec/moneta/adapter_localmemcache_spec.rb +1 -1
  67. data/spec/moneta/adapter_lruhash_spec.rb +1 -1
  68. data/spec/moneta/adapter_memcached_dalli_spec.rb +1 -1
  69. data/spec/moneta/adapter_memcached_dalli_with_default_expires_spec.rb +1 -1
  70. data/spec/moneta/adapter_memcached_native_spec.rb +1 -1
  71. data/spec/moneta/adapter_memcached_native_with_default_expires_spec.rb +1 -1
  72. data/spec/moneta/adapter_memcached_spec.rb +1 -1
  73. data/spec/moneta/adapter_memcached_with_default_expires_spec.rb +1 -1
  74. data/spec/moneta/adapter_mongo_moped_spec.rb +56 -0
  75. data/spec/moneta/adapter_mongo_moped_with_default_expires_spec.rb +51 -0
  76. data/spec/moneta/adapter_mongo_official_spec.rb +56 -0
  77. data/spec/moneta/adapter_mongo_official_with_default_expires_spec.rb +51 -0
  78. data/spec/moneta/adapter_mongo_spec.rb +1 -7
  79. data/spec/moneta/adapter_mongo_with_default_expires_spec.rb +1 -1
  80. data/spec/moneta/adapter_pstore_spec.rb +1 -1
  81. data/spec/moneta/adapter_redis_with_default_expires_spec.rb +1 -1
  82. data/spec/moneta/adapter_restclient_spec.rb +1 -1
  83. data/spec/moneta/adapter_sdbm_spec.rb +1 -1
  84. data/spec/moneta/adapter_sequel_spec.rb +1 -1
  85. data/spec/moneta/adapter_sqlite_spec.rb +1 -1
  86. data/spec/moneta/adapter_tdb_spec.rb +1 -1
  87. data/spec/moneta/adapter_tokyocabinet_bdb_spec.rb +1 -1
  88. data/spec/moneta/adapter_tokyocabinet_hdb_spec.rb +1 -1
  89. data/spec/moneta/adapter_yaml_spec.rb +1 -1
  90. data/spec/moneta/cache_file_memory_spec.rb +1 -1
  91. data/spec/moneta/expires_file_spec.rb +3 -3
  92. data/spec/moneta/expires_memory_with_default_expires_spec.rb +1 -1
  93. data/spec/moneta/mutex_spec.rb +1 -1
  94. data/spec/moneta/optionmerger_spec.rb +26 -26
  95. data/spec/moneta/pool_spec.rb +1 -1
  96. data/spec/moneta/semaphore_spec.rb +1 -1
  97. data/spec/moneta/shared_tcp_spec.rb +2 -2
  98. data/spec/moneta/shared_unix_spec.rb +2 -2
  99. data/spec/moneta/stack_file_memory_spec.rb +1 -1
  100. data/spec/moneta/stack_memory_file_spec.rb +1 -1
  101. data/spec/moneta/standard_activerecord_spec.rb +1 -1
  102. data/spec/moneta/standard_activerecord_with_expires_spec.rb +1 -1
  103. data/spec/moneta/standard_cassandra_spec.rb +1 -1
  104. data/spec/moneta/standard_client_tcp_spec.rb +1 -1
  105. data/spec/moneta/standard_client_unix_spec.rb +2 -2
  106. data/spec/moneta/standard_couch_spec.rb +1 -1
  107. data/spec/moneta/standard_couch_with_expires_spec.rb +1 -1
  108. data/spec/moneta/standard_datamapper_spec.rb +2 -2
  109. data/spec/moneta/standard_datamapper_with_expires_spec.rb +2 -2
  110. data/spec/moneta/standard_datamapper_with_repository_spec.rb +2 -2
  111. data/spec/moneta/standard_daybreak_spec.rb +1 -1
  112. data/spec/moneta/standard_daybreak_with_expires_spec.rb +1 -1
  113. data/spec/moneta/standard_dbm_spec.rb +1 -1
  114. data/spec/moneta/standard_dbm_with_expires_spec.rb +1 -1
  115. data/spec/moneta/standard_file_spec.rb +1 -1
  116. data/spec/moneta/standard_file_with_expires_spec.rb +1 -1
  117. data/spec/moneta/standard_fog_spec.rb +4 -4
  118. data/spec/moneta/standard_fog_with_expires_spec.rb +5 -5
  119. data/spec/moneta/standard_gdbm_spec.rb +1 -1
  120. data/spec/moneta/standard_gdbm_with_expires_spec.rb +1 -1
  121. data/spec/moneta/standard_hashfile_spec.rb +1 -1
  122. data/spec/moneta/standard_hashfile_with_expires_spec.rb +1 -1
  123. data/spec/moneta/standard_hbase_spec.rb +1 -1
  124. data/spec/moneta/standard_hbase_with_expires_spec.rb +1 -1
  125. data/spec/moneta/standard_kyotocabinet_spec.rb +1 -1
  126. data/spec/moneta/standard_kyotocabinet_with_expires_spec.rb +1 -1
  127. data/spec/moneta/standard_leveldb_spec.rb +1 -1
  128. data/spec/moneta/standard_leveldb_with_expires_spec.rb +1 -1
  129. data/spec/moneta/standard_lmdb_spec.rb +1 -1
  130. data/spec/moneta/standard_lmdb_with_expires_spec.rb +1 -1
  131. data/spec/moneta/standard_localmemcache_spec.rb +1 -1
  132. data/spec/moneta/standard_localmemcache_with_expires_spec.rb +1 -1
  133. data/spec/moneta/standard_lruhash_spec.rb +1 -1
  134. data/spec/moneta/standard_lruhash_with_expires_spec.rb +1 -1
  135. data/spec/moneta/standard_memcached_dalli_spec.rb +1 -1
  136. data/spec/moneta/standard_memcached_native_spec.rb +1 -1
  137. data/spec/moneta/standard_memcached_spec.rb +1 -1
  138. data/spec/moneta/standard_memory_spec.rb +1 -1
  139. data/spec/moneta/standard_memory_with_compress_spec.rb +1 -1
  140. data/spec/moneta/standard_memory_with_expires_spec.rb +1 -1
  141. data/spec/moneta/standard_memory_with_json_key_serializer_spec.rb +1 -1
  142. data/spec/moneta/standard_memory_with_json_serializer_spec.rb +1 -1
  143. data/spec/moneta/standard_memory_with_json_value_serializer_spec.rb +1 -1
  144. data/spec/moneta/standard_memory_with_prefix_spec.rb +1 -1
  145. data/spec/moneta/standard_memory_with_snappy_compress_spec.rb +1 -1
  146. data/spec/moneta/standard_mongo_moped_spec.rb +255 -0
  147. data/spec/moneta/standard_mongo_official_spec.rb +255 -0
  148. data/spec/moneta/standard_mongo_spec.rb +1 -1
  149. data/spec/moneta/standard_null_spec.rb +1 -1
  150. data/spec/moneta/standard_pstore_spec.rb +1 -1
  151. data/spec/moneta/standard_pstore_with_expires_spec.rb +1 -1
  152. data/spec/moneta/standard_redis_spec.rb +1 -1
  153. data/spec/moneta/standard_restclient_spec.rb +1 -1
  154. data/spec/moneta/standard_riak_spec.rb +1 -1
  155. data/spec/moneta/standard_riak_with_expires_spec.rb +1 -1
  156. data/spec/moneta/standard_sdbm_spec.rb +1 -1
  157. data/spec/moneta/standard_sdbm_with_expires_spec.rb +1 -1
  158. data/spec/moneta/standard_sequel_spec.rb +1 -1
  159. data/spec/moneta/standard_sequel_with_expires_spec.rb +1 -1
  160. data/spec/moneta/standard_sqlite_spec.rb +1 -1
  161. data/spec/moneta/standard_sqlite_with_expires_spec.rb +1 -1
  162. data/spec/moneta/standard_tdb_spec.rb +1 -1
  163. data/spec/moneta/standard_tdb_with_expires_spec.rb +1 -1
  164. data/spec/moneta/standard_tokyocabinet_spec.rb +1 -1
  165. data/spec/moneta/standard_tokyocabinet_with_expires_spec.rb +1 -1
  166. data/spec/moneta/standard_tokyotyrant_spec.rb +1 -1
  167. data/spec/moneta/standard_tokyotyrant_with_expires_spec.rb +1 -1
  168. data/spec/moneta/standard_yaml_spec.rb +1 -1
  169. data/spec/moneta/standard_yaml_with_expires_spec.rb +1 -1
  170. data/spec/moneta/transformer_bencode_spec.rb +1 -1
  171. data/spec/moneta/transformer_bert_spec.rb +1 -1
  172. data/spec/moneta/transformer_bson_spec.rb +1 -1
  173. data/spec/moneta/transformer_bzip2_spec.rb +1 -1
  174. data/spec/moneta/transformer_json_spec.rb +1 -1
  175. data/spec/moneta/transformer_key_inspect_spec.rb +1 -1
  176. data/spec/moneta/transformer_key_marshal_spec.rb +1 -1
  177. data/spec/moneta/transformer_key_to_s_spec.rb +1 -1
  178. data/spec/moneta/transformer_key_yaml_spec.rb +1 -1
  179. data/spec/moneta/transformer_lz4_spec.rb +1 -1
  180. data/spec/moneta/transformer_lzma_spec.rb +1 -1
  181. data/spec/moneta/transformer_lzo_spec.rb +1 -1
  182. data/spec/moneta/transformer_marshal_base64_spec.rb +1 -1
  183. data/spec/moneta/transformer_marshal_city128_spec.rb +1 -1
  184. data/spec/moneta/transformer_marshal_city32_spec.rb +1 -1
  185. data/spec/moneta/transformer_marshal_city64_spec.rb +1 -1
  186. data/spec/moneta/transformer_marshal_escape_spec.rb +1 -1
  187. data/spec/moneta/transformer_marshal_hex_spec.rb +1 -1
  188. data/spec/moneta/transformer_marshal_hmac_spec.rb +1 -1
  189. data/spec/moneta/transformer_marshal_md5_spec.rb +1 -1
  190. data/spec/moneta/transformer_marshal_md5_spread_spec.rb +1 -1
  191. data/spec/moneta/transformer_marshal_prefix_spec.rb +1 -1
  192. data/spec/moneta/transformer_marshal_qp_spec.rb +1 -1
  193. data/spec/moneta/transformer_marshal_rmd160_spec.rb +1 -1
  194. data/spec/moneta/transformer_marshal_sha1_spec.rb +1 -1
  195. data/spec/moneta/transformer_marshal_sha256_spec.rb +1 -1
  196. data/spec/moneta/transformer_marshal_sha384_spec.rb +1 -1
  197. data/spec/moneta/transformer_marshal_sha512_spec.rb +1 -1
  198. data/spec/moneta/transformer_marshal_spec.rb +1 -1
  199. data/spec/moneta/transformer_marshal_truncate_spec.rb +1 -1
  200. data/spec/moneta/transformer_marshal_uuencode_spec.rb +1 -1
  201. data/spec/moneta/transformer_msgpack_spec.rb +1 -1
  202. data/spec/moneta/transformer_ox_spec.rb +1 -1
  203. data/spec/moneta/transformer_php_spec.rb +1 -1
  204. data/spec/moneta/transformer_quicklz_spec.rb +1 -1
  205. data/spec/moneta/transformer_snappy_spec.rb +1 -1
  206. data/spec/moneta/transformer_tnet_spec.rb +1 -1
  207. data/spec/moneta/transformer_value_marshal_spec.rb +1 -1
  208. data/spec/moneta/transformer_value_yaml_spec.rb +1 -1
  209. data/spec/moneta/transformer_yaml_spec.rb +1 -1
  210. data/spec/moneta/transformer_zlib_spec.rb +1 -1
  211. data/spec/moneta/weak_create_spec.rb +4 -4
  212. data/spec/moneta/weak_increment_spec.rb +4 -4
  213. data/spec/monetaspecs.rb +481 -481
  214. metadata +14 -185
@@ -4,21 +4,21 @@ module ActiveSupport
4
4
  class MonetaStore < Store
5
5
  def initialize(options = nil)
6
6
  raise ArgumentError, 'Option :store is required' unless @store = options.delete(:store)
7
- @store = ::Moneta.new(@store, :expires => true) if Symbol === @store
7
+ @store = ::Moneta.new(@store, expires: true) if Symbol === @store
8
8
  super(options)
9
9
  extend Strategy::LocalCache
10
10
  end
11
11
 
12
12
  def increment(key, amount = 1, options = nil)
13
13
  options = merged_options(options)
14
- instrument(:increment, key, :amount => amount) do
14
+ instrument(:increment, key, amount: amount) do
15
15
  @store.increment(namespaced_key(key, options), amount, moneta_options(options))
16
16
  end
17
17
  end
18
18
 
19
19
  def decrement(key, amount = 1, options = nil)
20
20
  options = merged_options(options)
21
- instrument(:decrement, key, :amount => amount) do
21
+ instrument(:decrement, key, amount: amount) do
22
22
  @store.increment(namespaced_key(key, options), -amount, moneta_options(options))
23
23
  end
24
24
  end
@@ -47,6 +47,8 @@ module Moneta
47
47
  autoload :MemcachedNative, 'moneta/adapters/memcached/native'
48
48
  autoload :Memory, 'moneta/adapters/memory'
49
49
  autoload :Mongo, 'moneta/adapters/mongo'
50
+ autoload :MongoMoped, 'moneta/adapters/mongo/moped'
51
+ autoload :MongoOfficial, 'moneta/adapters/mongo/official'
50
52
  autoload :Null, 'moneta/adapters/null'
51
53
  autoload :PStore, 'moneta/adapters/pstore'
52
54
  autoload :Redis, 'moneta/adapters/redis'
@@ -98,7 +100,7 @@ module Moneta
98
100
  serializer = options.include?(:serializer) ? options.delete(:serializer) : :marshal
99
101
  key_serializer = options.include?(:key_serializer) ? options.delete(:key_serializer) : serializer
100
102
  value_serializer = options.include?(:value_serializer) ? options.delete(:value_serializer) : serializer
101
- transformer = { :key => [key_serializer, :prefix], :value => [value_serializer], :prefix => options.delete(:prefix) }
103
+ transformer = { key: [key_serializer, :prefix], value: [value_serializer], prefix: options.delete(:prefix) }
102
104
  transformer[:value] << (Symbol === compress ? compress : :zlib) if compress
103
105
  raise ArgumentError, 'Name must be Symbol' unless Symbol === name
104
106
  case name
@@ -123,7 +125,7 @@ module Moneta
123
125
  a = Adapters.const_get(name).new(options)
124
126
  build do
125
127
  use :Logger, Hash === logger ? logger : {} if logger
126
- use :Expires, :expires => options[:expires] if !a.supports?(:expires) && expires
128
+ use :Expires, expires: options[:expires] if !a.supports?(:expires) && expires
127
129
  use :Transformer, transformer
128
130
  use :Lock if threadsafe
129
131
  adapter a
@@ -60,12 +60,12 @@ module Moneta
60
60
 
61
61
  table.connection_pool.with_connection do |conn|
62
62
  unless table.table_exists?
63
- conn.create_table(table.table_name, :id => false) do |t|
63
+ conn.create_table(table.table_name, id: false) do |t|
64
64
  # Do not use binary key (Issue #17)
65
- t.string :k, :null => false
65
+ t.string :k, null: false
66
66
  t.binary :v
67
67
  end
68
- conn.add_index(table.table_name, :k, :unique => true)
68
+ conn.add_index(table.table_name, :k, unique: true)
69
69
  end
70
70
  end
71
71
 
@@ -86,14 +86,14 @@ module Moneta
86
86
  # (see Proxy#key?)
87
87
  def key?(key, options = {})
88
88
  @table.connection_pool.with_connection do
89
- !@table.where(:k => key).empty?
89
+ !@table.where(k: key).empty?
90
90
  end
91
91
  end
92
92
 
93
93
  # (see Proxy#load)
94
94
  def load(key, options = {})
95
95
  @table.connection_pool.with_connection do
96
- record = @table.select(:v).where(:k => key).first
96
+ record = @table.select(:v).where(k: key).first
97
97
  record && record.v
98
98
  end
99
99
  end
@@ -101,7 +101,7 @@ module Moneta
101
101
  # (see Proxy#store)
102
102
  def store(key, value, options = {})
103
103
  @table.connection_pool.with_connection do
104
- record = @table.select(:k).where(:k => key).first_or_initialize
104
+ record = @table.select(:k).where(k: key).first_or_initialize
105
105
  record.v = value
106
106
  record.save
107
107
  value
@@ -114,7 +114,7 @@ module Moneta
114
114
  # (see Proxy#delete)
115
115
  def delete(key, options = {})
116
116
  @table.connection_pool.with_connection do
117
- if record = @table.where(:k => key).first
117
+ if record = @table.where(k: key).first
118
118
  record.destroy
119
119
  record.v
120
120
  end
@@ -125,7 +125,7 @@ module Moneta
125
125
  def increment(key, amount = 1, options = {})
126
126
  @table.connection_pool.with_connection do
127
127
  @table.transaction do
128
- if record = @table.where(:k => key).lock.first
128
+ if record = @table.where(k: key).lock.first
129
129
  value = Utils.to_int(record.v) + amount
130
130
  record.v = value.to_s
131
131
  record.save
@@ -33,12 +33,12 @@ module Moneta
33
33
  "#{options[:host] || '127.0.0.1'}:#{options[:port] || 9160}",
34
34
  options)
35
35
  unless @backend.keyspaces.include?(keyspace)
36
- cf_def = ::Cassandra::ColumnFamily.new(:keyspace => keyspace, :name => @cf.to_s)
37
- ks_def = ::Cassandra::Keyspace.new(:name => keyspace,
38
- :strategy_class => 'SimpleStrategy',
39
- :strategy_options => { 'replication_factor' => '1' },
40
- :replication_factor => 1,
41
- :cf_defs => [cf_def])
36
+ cf_def = ::Cassandra::ColumnFamily.new(keyspace: keyspace, name: @cf.to_s)
37
+ ks_def = ::Cassandra::Keyspace.new(name: keyspace,
38
+ strategy_class: 'SimpleStrategy',
39
+ strategy_options: { 'replication_factor' => '1' },
40
+ replication_factor: 1,
41
+ cf_defs: [cf_def])
42
42
  # Wait for keyspace to be created (issue #24)
43
43
  10.times do
44
44
  begin
@@ -68,14 +68,14 @@ module Moneta
68
68
  def load(key, options = {})
69
69
  if value = @backend.get(@cf, key)
70
70
  expires = expires_value(options, nil)
71
- @backend.insert(@cf, key, {'value' => value['value'] }, :ttl => expires || nil) if expires != nil
71
+ @backend.insert(@cf, key, {'value' => value['value'] }, ttl: expires || nil) if expires != nil
72
72
  value['value']
73
73
  end
74
74
  end
75
75
 
76
76
  # (see Proxy#store)
77
77
  def store(key, value, options = {})
78
- @backend.insert(@cf, key, {'value' => value}, :ttl => expires_value(options) || nil)
78
+ @backend.insert(@cf, key, {'value' => value}, ttl: expires_value(options) || nil)
79
79
  value
80
80
  end
81
81
 
@@ -30,7 +30,7 @@ module Moneta
30
30
  @value_field = options[:value_field] || 'value'
31
31
  @type_field = options[:type_field] || 'type'
32
32
  url = "http://#{options[:host] || '127.0.0.1'}:#{options[:port] || 5984}/#{options[:db] || 'moneta'}"
33
- @backend = options[:backend] || ::Faraday.new(:url => url)
33
+ @backend = options[:backend] || ::Faraday.new(url: url)
34
34
  create_db
35
35
  end
36
36
 
@@ -12,8 +12,8 @@ module Moneta
12
12
 
13
13
  class Store
14
14
  include ::DataMapper::Resource
15
- property :k, String, :key => true, :length => 255
16
- property :v, Text, :lazy => false
15
+ property :k, String, key: true, length: 255
16
+ property :v, Text, lazy: false
17
17
  self.raise_on_save_failure = true
18
18
  end
19
19
 
@@ -46,9 +46,9 @@ module Moneta
46
46
  def store(key, value, options = {})
47
47
  context do
48
48
  if record = Store.get(key)
49
- record.update(:k => key, :v => value)
49
+ record.update(k: key, v: value)
50
50
  else
51
- Store.create(:k => key, :v => value)
51
+ Store.create(k: key, v: value)
52
52
  end
53
53
  value
54
54
  end
@@ -60,7 +60,7 @@ module Moneta
60
60
  # (see Proxy#create)
61
61
  def create(key, value, options = {})
62
62
  context do
63
- Store.create(:k => key, :v => value)
63
+ Store.create(k: key, v: value)
64
64
  true
65
65
  end
66
66
  rescue
@@ -12,7 +12,7 @@ module Moneta
12
12
  @backend = options[:backend] ||
13
13
  begin
14
14
  raise ArgumentError, 'Option :file is required' unless options[:file]
15
- ::Daybreak::DB.new(options[:file], :serializer => ::Daybreak::Serializer::None)
15
+ ::Daybreak::DB.new(options[:file], serializer: ::Daybreak::Serializer::None)
16
16
  end
17
17
  end
18
18
 
@@ -16,7 +16,7 @@ module Moneta
16
16
  def initialize(options = {})
17
17
  raise ArgumentError, 'Option :dir is required' unless dir = options.delete(:dir)
18
18
  @backend = options[:backend] || ::Fog::Storage.new(options)
19
- @directory = @backend.directories.get(dir) || @backend.directories.create(:key => dir)
19
+ @directory = @backend.directories.get(dir) || @backend.directories.create(key: dir)
20
20
  end
21
21
 
22
22
  # (see Proxy#key?)
@@ -42,7 +42,7 @@ module Moneta
42
42
  # (see Proxy#store)
43
43
  def store(key, value, options = {})
44
44
  value = value.dup if value.frozen? # HACK: Fog needs unfrozen string
45
- @directory.files.create(options.merge(:key => key, :body => value))
45
+ @directory.files.create(options.merge(key: key, body: value))
46
46
  value
47
47
  end
48
48
 
@@ -26,7 +26,7 @@ module Moneta
26
26
  ::LMDB.new(dir, options)
27
27
  end
28
28
 
29
- @db = @backend.database(db, :create => true)
29
+ @db = @backend.database(db, create: true)
30
30
  end
31
31
 
32
32
  # (see Proxy#key?)
@@ -15,7 +15,7 @@ module Moneta
15
15
  @backend = options[:backend] ||
16
16
  begin
17
17
  raise ArgumentError, 'Option :file is required' unless options[:file]
18
- ::LocalMemCache.new(:filename => options[:file])
18
+ ::LocalMemCache.new(filename: options[:file])
19
19
  end
20
20
  end
21
21
 
@@ -30,14 +30,14 @@ module Moneta
30
30
  value = @backend.get(key)
31
31
  if value
32
32
  expires = expires_value(options, nil)
33
- @backend.set(key, value, expires || nil, :raw => true) if expires != nil
33
+ @backend.set(key, value, expires || nil, raw: true) if expires != nil
34
34
  value
35
35
  end
36
36
  end
37
37
 
38
38
  # (see Proxy#store)
39
39
  def store(key, value, options = {})
40
- @backend.set(key, value, expires_value(options) || nil, :raw => true)
40
+ @backend.set(key, value, expires_value(options) || nil, raw: true)
41
41
  value
42
42
  end
43
43
 
@@ -76,7 +76,7 @@ module Moneta
76
76
 
77
77
  # (see Defaults#create)
78
78
  def create(key, value, options = {})
79
- @backend.add(key, value, expires_value(options) || nil, :raw => true)
79
+ @backend.add(key, value, expires_value(options) || nil, raw: true)
80
80
  end
81
81
 
82
82
  # (see Proxy#close)
@@ -22,7 +22,7 @@ module Moneta
22
22
  self.default_expires = options.delete(:expires)
23
23
  @backend = options[:backend] ||
24
24
  begin
25
- options.merge!(:prefix_key => options.delete(:namespace)) if options[:namespace]
25
+ options.merge!(prefix_key: options.delete(:namespace)) if options[:namespace]
26
26
  # We don't want a limitation on the key charset. Therefore we use the binary protocol.
27
27
  # It is also faster.
28
28
  options[:binary_protocol] = true unless options.include?(:binary_protocol)
@@ -1,169 +1,11 @@
1
- require 'mongo'
2
-
3
1
  module Moneta
4
2
  module Adapters
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::Mongo.new
14
- # db['key'] = {a: 1, b: 2}
15
- #
16
- # @api public
17
- class Mongo
18
- include Defaults
19
- include ExpiresSupport
20
-
21
- supports :create, :increment
22
- attr_reader :backend
23
-
24
- # @param [Hash] options
25
- # @option options [String] :collection ('moneta') MongoDB collection name
26
- # @option options [String] :host ('127.0.0.1') MongoDB server host
27
- # @option options [String] :user Username used to authenticate
28
- # @option options [String] :password Password used to authenticate
29
- # @option options [Integer] :port (MongoDB default port) MongoDB server port
30
- # @option options [String] :db ('moneta') MongoDB database
31
- # @option options [Integer] :expires Default expiration time
32
- # @option options [String] :expires_field ('expiresAt') Document field to store expiration time
33
- # @option options [String] :value_field ('value') Document field to store value
34
- # @option options [String] :type_field ('type') Document field to store value type
35
- # @option options [::Mongo::MongoClient] :backend Use existing backend instance
36
- # @option options Other options passed to `Mongo::MongoClient#new`
37
- def initialize(options = {})
38
- self.default_expires = options.delete(:expires)
39
- collection = options.delete(:collection) || 'moneta'
40
- db = options.delete(:db) || 'moneta'
41
- @expires_field = options.delete(:expires_field) || 'expiresAt'
42
- @value_field = options.delete(:value_field) || 'value'
43
- @type_field = options.delete(:type_field) || 'type'
44
- @backend = options[:backend] ||
45
- begin
46
- host = options.delete(:host) || '127.0.0.1'
47
- port = options.delete(:port) || ::Mongo::MongoClient::DEFAULT_PORT
48
- user = options.delete(:user)
49
- password = options.delete(:password)
50
- ::Mongo::MongoClient.new(host, port, options)
51
- end
52
- db = @backend.db(db)
53
- db.authenticate(user, password, true) if user && password
54
- @collection = db.collection(collection)
55
- if @backend.server_version >= '2.2'
56
- @collection.ensure_index([[@expires_field, ::Mongo::ASCENDING]], :expireAfterSeconds => 0)
57
- else
58
- warn 'Moneta::Adapters::Mongo - You are using MongoDB version < 2.2, expired documents will not be deleted'
59
- end
60
- end
61
-
62
- # (see Proxy#load)
63
- def load(key, options = {})
64
- key = to_binary(key)
65
- doc = @collection.find_one('_id' => key)
66
- if doc && (!doc[@expires_field] || doc[@expires_field] >= Time.now)
67
- expires = expires_at(options, nil)
68
- @collection.update({ '_id' => key },
69
- # @expires_field must be a Time object (BSON date datatype)
70
- { '$set' => { @expires_field => expires || nil } }) if expires != nil
71
- doc_to_value(doc)
72
- end
73
- end
74
-
75
- # (see Proxy#store)
76
- def store(key, value, options = {})
77
- key = to_binary(key)
78
- @collection.update({ '_id' => key },
79
- value_to_doc(key, value, options),
80
- { :upsert => true })
81
- value
82
- end
83
-
84
- # (see Proxy#delete)
85
- def delete(key, options = {})
86
- value = load(key, options)
87
- @collection.remove('_id' => to_binary(key)) if value
88
- value
89
- end
90
-
91
- # (see Proxy#increment)
92
- def increment(key, amount = 1, options = {})
93
- @collection.find_and_modify(:query => { '_id' => to_binary(key) },
94
- :update => { '$inc' => { @value_field => amount } },
95
- :new => true,
96
- :upsert => true)[@value_field]
97
- end
98
-
99
- # (see Proxy#create)
100
- def create(key, value, options = {})
101
- key = to_binary(key)
102
- @collection.insert(value_to_doc(key, value, options))
103
- true
104
- rescue ::Mongo::OperationFailure => ex
105
- raise if ex.error_code != 11000 # duplicate key error
106
- false
107
- end
108
-
109
- # (see Proxy#clear)
110
- def clear(options = {})
111
- @collection.remove
112
- self
113
- end
114
-
115
- # (see Proxy#close)
116
- def close
117
- @backend.close
118
- nil
119
- end
120
-
121
- protected
122
-
123
- def doc_to_value(doc)
124
- case doc[@type_field]
125
- when 'Hash'
126
- doc = doc.dup
127
- doc.delete('_id')
128
- doc.delete(@type_field)
129
- doc.delete(@expires_field)
130
- doc
131
- when 'Number'
132
- doc[@value_field]
133
- else
134
- doc[@value_field].to_s
135
- end
136
- end
137
-
138
- def value_to_doc(key, value, options)
139
- case value
140
- when Hash
141
- value.merge('_id' => key,
142
- @type_field => 'Hash',
143
- # @expires_field must be a Time object (BSON date datatype)
144
- @expires_field => expires_at(options) || nil)
145
- when Float, Fixnum
146
- { '_id' => key,
147
- @type_field => 'Number',
148
- @value_field => value,
149
- # @expires_field must be a Time object (BSON date datatype)
150
- @expires_field => expires_at(options) || nil }
151
- when String
152
- intvalue = value.to_i
153
- { '_id' => key,
154
- @type_field => 'String',
155
- @value_field => intvalue.to_s == value ? intvalue : to_binary(value),
156
- # @expires_field must be a Time object (BSON date datatype)
157
- @expires_field => expires_at(options) || nil }
158
- else
159
- raise ArgumentError, "Invalid value type: #{value.class}"
160
- end
161
- end
162
-
163
- def to_binary(s)
164
- s = s.dup if s.frozen? # HACK: BSON::Binary needs unfrozen string
165
- ::BSON::Binary.new(s)
166
- end
3
+ begin
4
+ require 'moneta/adapters/mongo/official'
5
+ Mongo = MongoOfficial
6
+ rescue LoadError
7
+ require 'moneta/adapters/mongo/moped'
8
+ Mongo = MongoMoped
167
9
  end
168
10
  end
169
11
  end
@@ -0,0 +1,63 @@
1
+ module Moneta
2
+ module Adapters
3
+ # @api private
4
+ class MongoBase
5
+ include Defaults
6
+ include ExpiresSupport
7
+
8
+ supports :create, :increment
9
+ attr_reader :backend
10
+
11
+ DEFAULT_PORT = 27017
12
+
13
+ def initialize(options = {})
14
+ self.default_expires = options.delete(:expires)
15
+ @expires_field = options.delete(:expires_field) || 'expiresAt'
16
+ @value_field = options.delete(:value_field) || 'value'
17
+ @type_field = options.delete(:type_field) || 'type'
18
+ end
19
+
20
+ protected
21
+
22
+ def doc_to_value(doc)
23
+ case doc[@type_field]
24
+ when 'Hash'
25
+ doc = doc.dup
26
+ doc.delete('_id')
27
+ doc.delete(@type_field)
28
+ doc.delete(@expires_field)
29
+ doc
30
+ when 'Number'
31
+ doc[@value_field]
32
+ else
33
+ doc[@value_field].to_s
34
+ end
35
+ end
36
+
37
+ def value_to_doc(key, value, options)
38
+ case value
39
+ when Hash
40
+ value.merge('_id' => key,
41
+ @type_field => 'Hash',
42
+ # @expires_field must be a Time object (BSON date datatype)
43
+ @expires_field => expires_at(options) || nil)
44
+ when Float, Fixnum
45
+ { '_id' => key,
46
+ @type_field => 'Number',
47
+ @value_field => value,
48
+ # @expires_field must be a Time object (BSON date datatype)
49
+ @expires_field => expires_at(options) || nil }
50
+ when String
51
+ intvalue = value.to_i
52
+ { '_id' => key,
53
+ @type_field => 'String',
54
+ @value_field => intvalue.to_s == value ? intvalue : to_binary(value),
55
+ # @expires_field must be a Time object (BSON date datatype)
56
+ @expires_field => expires_at(options) || nil }
57
+ else
58
+ raise ArgumentError, "Invalid value type: #{value.class}"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end