moneta 1.4.2 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +35 -38
- data/CHANGES +9 -0
- data/CONTRIBUTORS +2 -0
- data/Gemfile +143 -55
- data/README.md +5 -4
- data/lib/moneta/adapter.rb +52 -0
- data/lib/moneta/adapters/activerecord.rb +77 -68
- data/lib/moneta/adapters/activesupportcache.rb +22 -31
- data/lib/moneta/adapters/cassandra.rb +114 -116
- data/lib/moneta/adapters/client.rb +17 -18
- data/lib/moneta/adapters/couch.rb +31 -26
- data/lib/moneta/adapters/datamapper.rb +9 -5
- data/lib/moneta/adapters/daybreak.rb +15 -21
- data/lib/moneta/adapters/dbm.rb +6 -12
- data/lib/moneta/adapters/file.rb +21 -13
- data/lib/moneta/adapters/fog.rb +5 -6
- data/lib/moneta/adapters/gdbm.rb +6 -12
- data/lib/moneta/adapters/hbase.rb +10 -12
- data/lib/moneta/adapters/kyotocabinet.rb +22 -27
- data/lib/moneta/adapters/leveldb.rb +14 -20
- data/lib/moneta/adapters/lmdb.rb +19 -22
- data/lib/moneta/adapters/localmemcache.rb +7 -13
- data/lib/moneta/adapters/lruhash.rb +20 -20
- data/lib/moneta/adapters/memcached/dalli.rb +25 -33
- data/lib/moneta/adapters/memcached/native.rb +14 -20
- data/lib/moneta/adapters/memory.rb +5 -7
- data/lib/moneta/adapters/mongo.rb +53 -52
- data/lib/moneta/adapters/pstore.rb +21 -27
- data/lib/moneta/adapters/redis.rb +42 -37
- data/lib/moneta/adapters/restclient.rb +17 -25
- data/lib/moneta/adapters/riak.rb +8 -9
- data/lib/moneta/adapters/sdbm.rb +6 -12
- data/lib/moneta/adapters/sequel/mysql.rb +8 -8
- data/lib/moneta/adapters/sequel/postgres.rb +17 -17
- data/lib/moneta/adapters/sequel/postgres_hstore.rb +47 -47
- data/lib/moneta/adapters/sequel/sqlite.rb +9 -9
- data/lib/moneta/adapters/sequel.rb +56 -65
- data/lib/moneta/adapters/sqlite.rb +37 -35
- data/lib/moneta/adapters/tdb.rb +8 -14
- data/lib/moneta/adapters/tokyocabinet.rb +19 -17
- data/lib/moneta/adapters/tokyotyrant.rb +29 -30
- data/lib/moneta/adapters/yaml.rb +1 -5
- data/lib/moneta/config.rb +101 -0
- data/lib/moneta/expires.rb +0 -1
- data/lib/moneta/expires_support.rb +3 -4
- data/lib/moneta/pool.rb +1 -1
- data/lib/moneta/proxy.rb +29 -0
- data/lib/moneta/server.rb +21 -14
- data/lib/moneta/version.rb +1 -1
- data/lib/moneta/wrapper.rb +5 -0
- data/lib/moneta.rb +2 -0
- data/moneta.gemspec +1 -0
- data/spec/moneta/adapters/client/client_helper.rb +4 -3
- data/spec/moneta/adapters/faraday_helper.rb +3 -2
- data/spec/moneta/adapters/lruhash/adapter_lruhash_spec.rb +10 -6
- data/spec/moneta/adapters/mongo/adapter_mongo_spec.rb +2 -2
- data/spec/moneta/config_spec.rb +219 -0
- data/spec/moneta/proxies/transformer/transformer_bson_spec.rb +3 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_escape_spec.rb +2 -0
- data/spec/rack/session_moneta_spec.rb +44 -25
- data/spec/restserver.rb +3 -14
- metadata +24 -6
data/lib/moneta/adapters/dbm.rb
CHANGED
@@ -4,23 +4,17 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# DBM backend (Berkeley DB)
|
6
6
|
# @api public
|
7
|
-
class DBM
|
8
|
-
include Defaults
|
7
|
+
class DBM < Adapter
|
9
8
|
include DBMAdapter
|
10
9
|
include IncrementSupport
|
11
10
|
include CreateSupport
|
12
11
|
include EachKeySupport
|
13
12
|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
begin
|
20
|
-
raise ArgumentError, 'Option :file is required' unless options[:file]
|
21
|
-
::DBM.new(options[:file])
|
22
|
-
end
|
23
|
-
end
|
13
|
+
# @!method initialize(options = {})
|
14
|
+
# @param [Hash] options
|
15
|
+
# @option options [String] :file Database file
|
16
|
+
# @option options [::DBM] :backend Use existing backend instance
|
17
|
+
backend { |file:| ::DBM.new(file) }
|
24
18
|
end
|
25
19
|
end
|
26
20
|
end
|
data/lib/moneta/adapters/file.rb
CHANGED
@@ -7,14 +7,18 @@ module Moneta
|
|
7
7
|
# @api public
|
8
8
|
class File
|
9
9
|
include Defaults
|
10
|
+
include Config
|
11
|
+
|
10
12
|
supports :create, :increment, :each_key
|
11
13
|
|
14
|
+
config :dir, required: true
|
15
|
+
|
12
16
|
# @param [Hash] options
|
13
17
|
# @option options [String] :dir Directory where files will be stored
|
14
18
|
def initialize(options = {})
|
15
|
-
|
16
|
-
FileUtils.mkpath(
|
17
|
-
raise "#{
|
19
|
+
configure(options)
|
20
|
+
FileUtils.mkpath(config.dir)
|
21
|
+
raise "#{config.dir} is not a directory" unless ::File.directory?(config.dir)
|
18
22
|
end
|
19
23
|
|
20
24
|
# (see Proxy#key?)
|
@@ -24,12 +28,16 @@ module Moneta
|
|
24
28
|
|
25
29
|
# (see Proxy#each_key)
|
26
30
|
def each_key(&block)
|
27
|
-
entries = ::Dir.entries(
|
28
|
-
|
29
|
-
|
31
|
+
entries = ::Dir.entries(config.dir).reject do |k|
|
32
|
+
::File.directory?(::File.join(config.dir, k))
|
33
|
+
end
|
30
34
|
|
31
|
-
|
32
|
-
|
35
|
+
if block_given?
|
36
|
+
entries.each { |k| yield(k) }
|
37
|
+
self
|
38
|
+
else
|
39
|
+
enum_for(:each_key) { ::Dir.entries(config.dir).length - 2 }
|
40
|
+
end
|
33
41
|
end
|
34
42
|
|
35
43
|
# (see Proxy#load)
|
@@ -41,7 +49,7 @@ module Moneta
|
|
41
49
|
|
42
50
|
# (see Proxy#store)
|
43
51
|
def store(key, value, options = {})
|
44
|
-
temp_file = ::File.join(
|
52
|
+
temp_file = ::File.join(config.dir, "value-#{$PROCESS_ID}-#{Thread.current.object_id}")
|
45
53
|
path = store_path(key)
|
46
54
|
FileUtils.mkpath(::File.dirname(path))
|
47
55
|
::File.open(temp_file, 'wb') { |f| f.write(value) }
|
@@ -63,9 +71,9 @@ module Moneta
|
|
63
71
|
|
64
72
|
# (see Proxy#clear)
|
65
73
|
def clear(options = {})
|
66
|
-
temp_dir = "#{
|
67
|
-
::File.rename(
|
68
|
-
FileUtils.mkpath(
|
74
|
+
temp_dir = "#{config.dir}-#{$PROCESS_ID}-#{Thread.current.object_id}"
|
75
|
+
::File.rename(config.dir, temp_dir)
|
76
|
+
FileUtils.mkpath(config.dir)
|
69
77
|
self
|
70
78
|
rescue Errno::ENOENT
|
71
79
|
self
|
@@ -120,7 +128,7 @@ module Moneta
|
|
120
128
|
protected
|
121
129
|
|
122
130
|
def store_path(key)
|
123
|
-
::File.join(
|
131
|
+
::File.join(config.dir, key)
|
124
132
|
end
|
125
133
|
end
|
126
134
|
end
|
data/lib/moneta/adapters/fog.rb
CHANGED
@@ -4,19 +4,18 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# Fog backend (Cloud storage services)
|
6
6
|
# @api public
|
7
|
-
class Fog
|
8
|
-
|
7
|
+
class Fog < Adapter
|
8
|
+
config :dir, required: true
|
9
9
|
|
10
|
-
|
10
|
+
backend { |**options| ::Fog::Storage.new(options) }
|
11
11
|
|
12
12
|
# @param [Hash] options
|
13
13
|
# @option options [String] :dir Fog directory
|
14
14
|
# @option options [::Fog::Storage] :backend Use existing backend instance
|
15
15
|
# @option options Other options passed to `Fog::Storage#new`
|
16
16
|
def initialize(options = {})
|
17
|
-
|
18
|
-
@
|
19
|
-
@directory = @backend.directories.get(dir) || @backend.directories.create(key: dir)
|
17
|
+
super
|
18
|
+
@directory = backend.directories.get(config.dir) || backend.directories.create(key: config.dir)
|
20
19
|
end
|
21
20
|
|
22
21
|
# (see Proxy#key?)
|
data/lib/moneta/adapters/gdbm.rb
CHANGED
@@ -4,23 +4,17 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# GDBM backend
|
6
6
|
# @api public
|
7
|
-
class GDBM
|
8
|
-
include Defaults
|
7
|
+
class GDBM < Adapter
|
9
8
|
include DBMAdapter
|
10
9
|
include IncrementSupport
|
11
10
|
include CreateSupport
|
12
11
|
include EachKeySupport
|
13
12
|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
begin
|
20
|
-
raise ArgumentError, 'Option :file is required' unless options[:file]
|
21
|
-
::GDBM.new(options[:file])
|
22
|
-
end
|
23
|
-
end
|
13
|
+
# @!method initialize(options = {})
|
14
|
+
# @param [Hash] options
|
15
|
+
# @option options [String] :file Database file
|
16
|
+
# @option options [::GDBM] :backend Use existing backend instance
|
17
|
+
backend { |file:| ::GDBM.new(file) }
|
24
18
|
end
|
25
19
|
end
|
26
20
|
end
|
@@ -4,10 +4,12 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# HBase thrift backend
|
6
6
|
# @api public
|
7
|
-
class HBase
|
8
|
-
|
7
|
+
class HBase < Adapter
|
8
|
+
config :column, 'value'
|
9
|
+
config :table, 'moneta'
|
10
|
+
config :column_family, 'moneta'
|
9
11
|
|
10
|
-
|
12
|
+
backend { |host: '127.0.0.1', port: 9090| HBaseRb::Client.new(host, port) }
|
11
13
|
|
12
14
|
# TODO: Add create support using checkAndPut if added to thrift api
|
13
15
|
# https://issues.apache.org/jira/browse/HBASE-3307
|
@@ -22,14 +24,10 @@ module Moneta
|
|
22
24
|
# @option options [String] :column ('value') Column
|
23
25
|
# @option options [::HBaseRb::Client] :backend Use existing backend instance
|
24
26
|
def initialize(options = {})
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@
|
29
|
-
@backend = options[:backend] ||
|
30
|
-
HBaseRb::Client.new(options[:host] || '127.0.0.1', options[:port] || '9090')
|
31
|
-
@backend.create_table(options[:table], cf) unless @backend.has_table?(options[:table])
|
32
|
-
@table = @backend.get_table(options[:table])
|
27
|
+
super
|
28
|
+
@column = [config.column_family, config.column].join(':')
|
29
|
+
backend.create_table(config.table, config.column_family) unless backend.has_table?(config.table)
|
30
|
+
@table = backend.get_table(config.table)
|
33
31
|
end
|
34
32
|
|
35
33
|
# (see Proxy#key?)
|
@@ -75,7 +73,7 @@ module Moneta
|
|
75
73
|
|
76
74
|
# (see Proxy#close)
|
77
75
|
def close
|
78
|
-
|
76
|
+
backend.close
|
79
77
|
nil
|
80
78
|
end
|
81
79
|
|
@@ -4,58 +4,53 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# KyotoCabinet backend
|
6
6
|
# @api public
|
7
|
-
class KyotoCabinet
|
8
|
-
include Defaults
|
7
|
+
class KyotoCabinet < Adapter
|
9
8
|
include HashAdapter
|
10
9
|
|
11
10
|
supports :each_key, :increment, :create
|
12
11
|
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
@backend = ::KyotoCabinet::DB.new
|
22
|
-
raise @backend.error.to_s unless @backend.open(options[:file],
|
23
|
-
::KyotoCabinet::DB::OWRITER | ::KyotoCabinet::DB::OCREATE)
|
24
|
-
end
|
12
|
+
# @!method initialize(options = {})
|
13
|
+
# @param [Hash] options
|
14
|
+
# @option options [String] :file Database file
|
15
|
+
# @option options [::KyotoCabinet::DB] :backend Use existing backend instance
|
16
|
+
backend do |file:|
|
17
|
+
backend = ::KyotoCabinet::DB.new
|
18
|
+
raise backend.error.to_s unless backend.open(file, ::KyotoCabinet::DB::OWRITER | ::KyotoCabinet::DB::OCREATE)
|
19
|
+
backend
|
25
20
|
end
|
26
21
|
|
27
22
|
# (see Proxy#key?)
|
28
23
|
def key?(key, options = {})
|
29
|
-
|
24
|
+
backend.check(key) >= 0
|
30
25
|
end
|
31
26
|
|
32
27
|
# (see Proxy#delete)
|
33
28
|
def delete(key, options = {})
|
34
|
-
|
29
|
+
backend.seize(key)
|
35
30
|
end
|
36
31
|
|
37
32
|
# (see Proxy#create)
|
38
33
|
def create(key, value, options = {})
|
39
|
-
|
34
|
+
backend.add(key, value)
|
40
35
|
end
|
41
36
|
|
42
37
|
# (see Proxy#close)
|
43
38
|
def close
|
44
|
-
|
39
|
+
backend.close
|
45
40
|
nil
|
46
41
|
end
|
47
42
|
|
48
43
|
# (see Proxy#each_key)
|
49
44
|
def each_key
|
50
|
-
return enum_for(:each_key) {
|
51
|
-
|
45
|
+
return enum_for(:each_key) { backend.count } unless block_given?
|
46
|
+
backend.each_key { |arr| yield arr[0] }
|
52
47
|
self
|
53
48
|
end
|
54
49
|
|
55
50
|
# (see Proxy#increment)
|
56
51
|
def increment(key, amount = 1, options = {})
|
57
52
|
ret = nil
|
58
|
-
success =
|
53
|
+
success = backend.accept(key) do |_, value|
|
59
54
|
ret =
|
60
55
|
if value
|
61
56
|
Integer(value) + amount
|
@@ -65,13 +60,13 @@ module Moneta
|
|
65
60
|
ret.to_s
|
66
61
|
end
|
67
62
|
|
68
|
-
raise
|
63
|
+
raise backend.error unless success
|
69
64
|
ret
|
70
65
|
end
|
71
66
|
|
72
67
|
# (see Proxy#slice)
|
73
68
|
def slice(*keys, atomic: true, **options)
|
74
|
-
|
69
|
+
backend.get_bulk(keys, atomic)
|
75
70
|
end
|
76
71
|
|
77
72
|
# (see Proxy#values_at)
|
@@ -87,7 +82,7 @@ module Moneta
|
|
87
82
|
|
88
83
|
success =
|
89
84
|
if block_given?
|
90
|
-
|
85
|
+
backend.transaction(hard) do
|
91
86
|
existing = slice(*pairs.map { |k, _| k }, **options)
|
92
87
|
pairs = pairs.map do |key, new_value|
|
93
88
|
if existing.key?(key)
|
@@ -96,13 +91,13 @@ module Moneta
|
|
96
91
|
[key, new_value]
|
97
92
|
end
|
98
93
|
end
|
99
|
-
|
94
|
+
backend.set_bulk(pairs.to_h, atomic) >= 0
|
100
95
|
end
|
101
96
|
else
|
102
|
-
|
97
|
+
backend.set_bulk(pairs.to_h, atomic) >= 0
|
103
98
|
end
|
104
99
|
|
105
|
-
raise
|
100
|
+
raise backend.error unless success
|
106
101
|
self
|
107
102
|
end
|
108
103
|
end
|
@@ -4,59 +4,53 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# LevelDB backend
|
6
6
|
# @api public
|
7
|
-
class LevelDB
|
8
|
-
include Defaults
|
7
|
+
class LevelDB < Adapter
|
9
8
|
include HashAdapter
|
10
9
|
include IncrementSupport
|
11
10
|
include CreateSupport
|
12
11
|
include EachKeySupport
|
13
12
|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
begin
|
21
|
-
raise ArgumentError, 'Option :dir is required' unless options[:dir]
|
22
|
-
::LevelDB::DB.new(options[:dir])
|
23
|
-
end
|
24
|
-
end
|
13
|
+
# @!method initialize(options = {})
|
14
|
+
# @param [Hash] options
|
15
|
+
# @option options [String] :dir - Database path
|
16
|
+
# @option options All other options passed to `LevelDB::DB#new`
|
17
|
+
# @option options [::LevelDB::DB] :backend Use existing backend instance
|
18
|
+
backend { |dir:| ::LevelDB::DB.new(dir) }
|
25
19
|
|
26
20
|
# (see Proxy#key?)
|
27
21
|
def key?(key, options = {})
|
28
|
-
|
22
|
+
backend.includes?(key)
|
29
23
|
end
|
30
24
|
|
31
25
|
# (see Proxy#clear)
|
32
26
|
def clear(options = {})
|
33
|
-
|
27
|
+
backend.each { |k,| delete(k, options) }
|
34
28
|
self
|
35
29
|
end
|
36
30
|
|
37
31
|
# (see Proxy#close)
|
38
32
|
def close
|
39
|
-
|
33
|
+
backend.close
|
40
34
|
nil
|
41
35
|
end
|
42
36
|
|
43
37
|
# (see Proxy#each_key)
|
44
38
|
def each_key
|
45
|
-
return enum_for(:each_key) {
|
46
|
-
|
39
|
+
return enum_for(:each_key) { backend.size } unless block_given?
|
40
|
+
backend.each { |key, _| yield key }
|
47
41
|
self
|
48
42
|
end
|
49
43
|
|
50
44
|
# (see Proxy#values_at)
|
51
45
|
def values_at(*keys, **options)
|
52
46
|
ret = nil
|
53
|
-
|
47
|
+
backend.batch { ret = super }
|
54
48
|
ret
|
55
49
|
end
|
56
50
|
|
57
51
|
# (see Proxy#merge!)
|
58
52
|
def merge!(*keys, **options)
|
59
|
-
|
53
|
+
backend.batch { super }
|
60
54
|
self
|
61
55
|
end
|
62
56
|
end
|
data/lib/moneta/adapters/lmdb.rb
CHANGED
@@ -5,28 +5,25 @@ module Moneta
|
|
5
5
|
module Adapters
|
6
6
|
# LMDB backend
|
7
7
|
# @api public
|
8
|
-
class LMDB
|
9
|
-
include Defaults
|
10
|
-
|
8
|
+
class LMDB < Adapter
|
11
9
|
supports :create, :increment, :each_key
|
12
|
-
attr_reader :backend, :db
|
13
10
|
|
14
11
|
PUT_FLAGS = %i[nooverwrite nodupdata current append appenddup].freeze
|
15
12
|
|
13
|
+
config :db, default: 'moneta'
|
14
|
+
|
15
|
+
backend do |dir:, **options|
|
16
|
+
FileUtils.mkpath(dir)
|
17
|
+
::LMDB.new(dir, options)
|
18
|
+
end
|
19
|
+
|
16
20
|
# @param [Hash] options
|
17
21
|
# @option options [String] :dir Environment directory
|
18
22
|
# @option options [::LMDB::Environment] :backend Use existing backend instance
|
19
|
-
# @option options [String or nil] :db Database name
|
20
|
-
def initialize(options)
|
21
|
-
|
22
|
-
@
|
23
|
-
begin
|
24
|
-
raise ArgumentError, 'Option :dir is required' unless dir = options.delete(:dir)
|
25
|
-
FileUtils.mkpath(dir)
|
26
|
-
::LMDB.new(dir, options)
|
27
|
-
end
|
28
|
-
|
29
|
-
@db = @backend.database(db, create: true)
|
23
|
+
# @option options [String or nil] :db ('moneta') Database name
|
24
|
+
def initialize(options = {})
|
25
|
+
super
|
26
|
+
@db = backend.database(config.db, create: true)
|
30
27
|
end
|
31
28
|
|
32
29
|
# (see Proxy#key?)
|
@@ -47,7 +44,7 @@ module Moneta
|
|
47
44
|
|
48
45
|
# (see Proxy#delete)
|
49
46
|
def delete(key, options = {})
|
50
|
-
|
47
|
+
backend.transaction do
|
51
48
|
if value = @db.get(key)
|
52
49
|
@db.delete(key)
|
53
50
|
value
|
@@ -63,7 +60,7 @@ module Moneta
|
|
63
60
|
|
64
61
|
# (see Proxy#increment)
|
65
62
|
def increment(key, amount = 1, options = {})
|
66
|
-
|
63
|
+
backend.transaction do
|
67
64
|
value = Integer(@db.get(key) || 0) + amount
|
68
65
|
@db.put(key, value.to_s, Utils.only(options, *PUT_FLAGS))
|
69
66
|
value
|
@@ -72,7 +69,7 @@ module Moneta
|
|
72
69
|
|
73
70
|
# (see Defaults#create)
|
74
71
|
def create(key, value, options = {})
|
75
|
-
|
72
|
+
backend.transaction do
|
76
73
|
if @db.get(key)
|
77
74
|
false
|
78
75
|
else
|
@@ -84,7 +81,7 @@ module Moneta
|
|
84
81
|
|
85
82
|
# (see Proxy#close)
|
86
83
|
def close
|
87
|
-
|
84
|
+
backend.close
|
88
85
|
nil
|
89
86
|
end
|
90
87
|
|
@@ -103,17 +100,17 @@ module Moneta
|
|
103
100
|
|
104
101
|
# (see Proxy#values_at)
|
105
102
|
def values_at(*keys, **options)
|
106
|
-
|
103
|
+
backend.transaction { super }
|
107
104
|
end
|
108
105
|
|
109
106
|
# (see Proxy#slice)
|
110
107
|
def slice(*keys, **options)
|
111
|
-
|
108
|
+
backend.transaction { super }
|
112
109
|
end
|
113
110
|
|
114
111
|
# (see Proxy#merge!)
|
115
112
|
def merge!(pairs, options = {})
|
116
|
-
|
113
|
+
backend.transaction { super }
|
117
114
|
end
|
118
115
|
end
|
119
116
|
end
|
@@ -4,25 +4,19 @@ module Moneta
|
|
4
4
|
module Adapters
|
5
5
|
# LocalMemCache backend
|
6
6
|
# @api public
|
7
|
-
class LocalMemCache
|
8
|
-
include Defaults
|
7
|
+
class LocalMemCache < Adapter
|
9
8
|
include HashAdapter
|
10
9
|
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
begin
|
17
|
-
raise ArgumentError, 'Option :file is required' unless options[:file]
|
18
|
-
::LocalMemCache.new(filename: options[:file])
|
19
|
-
end
|
20
|
-
end
|
10
|
+
# @!method initialize(options = {})
|
11
|
+
# @param [Hash] options
|
12
|
+
# @option options [String] :file Database file
|
13
|
+
# @option options [::LocalMemCache] :backend Use existing backend instance
|
14
|
+
backend { |file:| ::LocalMemCache.new(filename: file) }
|
21
15
|
|
22
16
|
# (see Proxy#delete)
|
23
17
|
def delete(key, options = {})
|
24
18
|
value = load(key, options)
|
25
|
-
|
19
|
+
backend.delete(key)
|
26
20
|
value
|
27
21
|
end
|
28
22
|
end
|
@@ -6,70 +6,70 @@ module Moneta
|
|
6
6
|
# both memory usage and hash size.
|
7
7
|
#
|
8
8
|
# @api public
|
9
|
-
class LRUHash
|
10
|
-
include Defaults
|
9
|
+
class LRUHash < Adapter
|
11
10
|
include IncrementSupport
|
12
11
|
include CreateSupport
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
config :max_size, default: 1024000
|
14
|
+
config(:max_value) { |max_size:, max_value:, **| [max_value, max_size].compact.min }
|
15
|
+
config :max_count, default: 10240
|
16
16
|
|
17
17
|
supports :each_key
|
18
18
|
|
19
|
+
backend { {} }
|
20
|
+
|
19
21
|
# @param [Hash] options
|
20
22
|
# @option options [Integer] :max_size (1024000) Maximum byte size of all values, nil disables the limit
|
21
23
|
# @option options [Integer] :max_value (options[:max_size]) Maximum byte size of one value, nil disables the limit
|
22
24
|
# @option options [Integer] :max_count (10240) Maximum number of values, nil disables the limit
|
23
25
|
def initialize(options = {})
|
24
|
-
|
25
|
-
@max_count = options.fetch(:max_count) { DEFAULT_MAX_COUNT }
|
26
|
-
@max_value = [options[:max_value], @max_size].compact.min
|
26
|
+
super
|
27
27
|
clear
|
28
28
|
end
|
29
29
|
|
30
30
|
# (see Proxy#key?)
|
31
31
|
def key?(key, options = {})
|
32
|
-
|
32
|
+
backend.key?(key)
|
33
33
|
end
|
34
34
|
|
35
35
|
# (see Proxy#each_key)
|
36
36
|
def each_key(&block)
|
37
|
-
return enum_for(:each_key) {
|
37
|
+
return enum_for(:each_key) { backend.length } unless block_given?
|
38
38
|
|
39
39
|
# The backend needs to be duplicated because reading mutates this
|
40
40
|
# store.
|
41
|
-
|
41
|
+
backend.dup.each_key { |k| yield(k) }
|
42
42
|
self
|
43
43
|
end
|
44
44
|
|
45
45
|
# (see Proxy#load)
|
46
46
|
def load(key, options = {})
|
47
|
-
if value =
|
48
|
-
|
47
|
+
if value = backend.delete(key)
|
48
|
+
backend[key] = value
|
49
49
|
value
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
# (see Proxy#store)
|
54
54
|
def store(key, value, options = {})
|
55
|
-
if
|
55
|
+
if config.max_value && value.bytesize > config.max_value
|
56
56
|
delete(key)
|
57
57
|
else
|
58
|
-
if
|
59
|
-
if old_value =
|
58
|
+
if config.max_size
|
59
|
+
if old_value = backend.delete(key)
|
60
60
|
@size -= old_value.bytesize
|
61
61
|
end
|
62
62
|
@size += value.bytesize
|
63
63
|
end
|
64
|
-
|
65
|
-
drop while
|
64
|
+
backend[key] = value
|
65
|
+
drop while config.max_size && @size > config.max_size || config.max_count && backend.size > config.max_count
|
66
66
|
end
|
67
67
|
value
|
68
68
|
end
|
69
69
|
|
70
70
|
# (see Proxy#delete)
|
71
71
|
def delete(key, options = {})
|
72
|
-
if value =
|
72
|
+
if value = backend.delete(key) and config.max_size
|
73
73
|
@size -= value.bytesize
|
74
74
|
end
|
75
75
|
value
|
@@ -77,7 +77,7 @@ module Moneta
|
|
77
77
|
|
78
78
|
# (see Proxy#clear)
|
79
79
|
def clear(options = {})
|
80
|
-
|
80
|
+
backend.clear
|
81
81
|
@size = 0
|
82
82
|
self
|
83
83
|
end
|
@@ -87,7 +87,7 @@ module Moneta
|
|
87
87
|
# @param [Hash] options Options to merge
|
88
88
|
# @return [(Object, String), nil] The dropped pair, if any
|
89
89
|
def drop(options = {})
|
90
|
-
if key =
|
90
|
+
if key = backend.keys.first
|
91
91
|
[key, delete(key)]
|
92
92
|
end
|
93
93
|
end
|