mankai-cache 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +23 -0
- data/LICENSE +20 -0
- data/README.md +368 -0
- data/lib/mankai/cache/coder.rb +102 -0
- data/lib/mankai/cache/entry.rb +17 -0
- data/lib/mankai/cache/errors.rb +34 -0
- data/lib/mankai/cache/instrumenter.rb +92 -0
- data/lib/mankai/cache/key.rb +56 -0
- data/lib/mankai/cache/provider_source.rb +161 -0
- data/lib/mankai/cache/serializers/json.rb +27 -0
- data/lib/mankai/cache/serializers/marshal.rb +25 -0
- data/lib/mankai/cache/serializers.rb +39 -0
- data/lib/mankai/cache/store.rb +61 -0
- data/lib/mankai/cache/stores/file.rb +189 -0
- data/lib/mankai/cache/stores/memory.rb +145 -0
- data/lib/mankai/cache/stores/null.rb +53 -0
- data/lib/mankai/cache/testing/store_examples.rb +69 -0
- data/lib/mankai/cache/version.rb +9 -0
- data/lib/mankai/cache/view_helpers.rb +54 -0
- data/lib/mankai/cache.rb +348 -0
- data/lib/mankai-cache.rb +3 -0
- data/mankai-cache.gemspec +34 -0
- metadata +100 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
class Cache
|
|
7
|
+
# Turns the keys callers pass into strings, and finds their versions.
|
|
8
|
+
#
|
|
9
|
+
# A key can be a String, Symbol, number, boolean, nil, Time or Date; an object that responds
|
|
10
|
+
# to `cache_key`; or an Array or Hash of those. Arrays join their parts with "/", so
|
|
11
|
+
# `["book", 1]` is "book/1". Anything else raises InvalidKeyError rather than falling back to
|
|
12
|
+
# `to_s`, which for most objects includes a memory address and would never hit.
|
|
13
|
+
#
|
|
14
|
+
# An object that also responds to `cache_version` versions the entry: a new version makes
|
|
15
|
+
# the old entry a miss, so the same key can be reused instead of piling up new ones.
|
|
16
|
+
#
|
|
17
|
+
# @api public
|
|
18
|
+
# @since 0.1.0
|
|
19
|
+
module Key
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# @return [String]
|
|
23
|
+
# @raise [InvalidKeyError]
|
|
24
|
+
#
|
|
25
|
+
# @api public
|
|
26
|
+
# @since 0.1.0
|
|
27
|
+
def expand(key)
|
|
28
|
+
case key
|
|
29
|
+
when String then key
|
|
30
|
+
when Symbol, Numeric, true, false, nil, Date then key.to_s
|
|
31
|
+
when Time then key.getutc.strftime("%Y%m%d%H%M%S%6N")
|
|
32
|
+
when Array then key.map { |part| expand(part) }.join("/")
|
|
33
|
+
when Hash then key.sort_by { |name, _| name.to_s }.map { |name, value| "#{name}=#{expand(value)}" }.join("/")
|
|
34
|
+
else
|
|
35
|
+
return key.cache_key.to_s if key.respond_to?(:cache_key)
|
|
36
|
+
|
|
37
|
+
raise InvalidKeyError, "cannot build a cache key from #{key.class}; give it a #cache_key method or " \
|
|
38
|
+
"pass a String, Symbol, number, Time, Array or Hash"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [String, nil] the versions of the key's parts that have one, joined with "/"
|
|
43
|
+
#
|
|
44
|
+
# @api public
|
|
45
|
+
# @since 0.1.0
|
|
46
|
+
def version(key)
|
|
47
|
+
if key.is_a?(Array)
|
|
48
|
+
versions = key.filter_map { |part| version(part) }
|
|
49
|
+
versions.empty? ? nil : versions.join("/")
|
|
50
|
+
elsif key.respond_to?(:cache_version)
|
|
51
|
+
key.cache_version&.to_s
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry/system"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
class Cache
|
|
7
|
+
# dry-system provider source that registers a cache as the "cache" component.
|
|
8
|
+
#
|
|
9
|
+
# In a Hanami app:
|
|
10
|
+
#
|
|
11
|
+
# # config/providers/cache.rb
|
|
12
|
+
# require "mankai/cache"
|
|
13
|
+
#
|
|
14
|
+
# Hanami.app.register_provider(:cache, from: :mankai_cache) do
|
|
15
|
+
# configure do |config|
|
|
16
|
+
# config.store = Hanami.env?(:test) ? :memory : :file
|
|
17
|
+
# config.expires_in = 3600
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# Then `include Deps["cache"]` wherever you need it.
|
|
22
|
+
#
|
|
23
|
+
# @api public
|
|
24
|
+
# @since 0.1.0
|
|
25
|
+
class ProviderSource < Dry::System::Provider::Source
|
|
26
|
+
# @api private
|
|
27
|
+
# @since 0.1.0
|
|
28
|
+
BUILT_IN_STORES = {
|
|
29
|
+
memory: ->(**options) { Stores::Memory.new(**options) },
|
|
30
|
+
file: ->(**options) { Stores::File.new(**options) },
|
|
31
|
+
null: ->(**) { Stores::Null.new }
|
|
32
|
+
}.freeze
|
|
33
|
+
|
|
34
|
+
# :memory, :file or :null; a store object; or a factory (responds to call(container)) such
|
|
35
|
+
# as Mankai::Cache::SQL.store(...).
|
|
36
|
+
#
|
|
37
|
+
# @api public
|
|
38
|
+
# @since 0.1.0
|
|
39
|
+
setting :store, default: :memory
|
|
40
|
+
|
|
41
|
+
# Keyword options for a built-in store, e.g. {size: 64 * 1024 * 1024} for :memory or
|
|
42
|
+
# {path: "/var/cache/app"} for :file (default: the app's tmp/cache).
|
|
43
|
+
#
|
|
44
|
+
# @api public
|
|
45
|
+
# @since 0.1.0
|
|
46
|
+
setting :store_options, default: {}
|
|
47
|
+
|
|
48
|
+
# Prefixed to every key.
|
|
49
|
+
#
|
|
50
|
+
# @api public
|
|
51
|
+
# @since 0.1.0
|
|
52
|
+
setting :namespace, default: nil
|
|
53
|
+
|
|
54
|
+
# Default lifetime of entries, in seconds. nil keeps them until the store evicts them.
|
|
55
|
+
#
|
|
56
|
+
# @api public
|
|
57
|
+
# @since 0.1.0
|
|
58
|
+
setting :expires_in, default: nil
|
|
59
|
+
|
|
60
|
+
# :marshal, :json, or an object with dump and load (see Serializers).
|
|
61
|
+
#
|
|
62
|
+
# @api public
|
|
63
|
+
# @since 0.1.0
|
|
64
|
+
setting :serializer, default: :marshal
|
|
65
|
+
|
|
66
|
+
# Deflate values of at least compress_threshold bytes.
|
|
67
|
+
#
|
|
68
|
+
# @api public
|
|
69
|
+
# @since 0.1.0
|
|
70
|
+
setting :compress, default: true
|
|
71
|
+
# @api public
|
|
72
|
+
# @since 0.1.0
|
|
73
|
+
setting :compress_threshold, default: 1024
|
|
74
|
+
|
|
75
|
+
# @api public
|
|
76
|
+
# @since 0.1.0
|
|
77
|
+
setting :max_key_size, default: DEFAULT_MAX_KEY_SIZE
|
|
78
|
+
|
|
79
|
+
# Container key of the dry-monitor notifications bus, used when present.
|
|
80
|
+
#
|
|
81
|
+
# @api public
|
|
82
|
+
# @since 0.1.0
|
|
83
|
+
setting :notifications_key, default: "notifications"
|
|
84
|
+
|
|
85
|
+
# Returns the current Time. Override in tests.
|
|
86
|
+
#
|
|
87
|
+
# @api public
|
|
88
|
+
# @since 0.1.0
|
|
89
|
+
setting :clock, default: -> { Time.now }
|
|
90
|
+
|
|
91
|
+
# @api private
|
|
92
|
+
# @since 0.1.0
|
|
93
|
+
def start
|
|
94
|
+
@cache = Cache.new(
|
|
95
|
+
store: build_store,
|
|
96
|
+
namespace: config.namespace,
|
|
97
|
+
expires_in: config.expires_in,
|
|
98
|
+
coder: build_coder,
|
|
99
|
+
max_key_size: config.max_key_size,
|
|
100
|
+
clock: config.clock,
|
|
101
|
+
instrumenter: build_instrumenter
|
|
102
|
+
)
|
|
103
|
+
register "cache", @cache
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# @api private
|
|
107
|
+
# @since 0.1.0
|
|
108
|
+
def stop
|
|
109
|
+
@cache&.close
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
# @api private
|
|
115
|
+
# @since 0.1.0
|
|
116
|
+
def build_coder
|
|
117
|
+
Coder.new(serializer: config.serializer, compress: config.compress,
|
|
118
|
+
compress_threshold: config.compress_threshold)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# @api private
|
|
122
|
+
# @since 0.1.0
|
|
123
|
+
def build_instrumenter
|
|
124
|
+
key = config.notifications_key
|
|
125
|
+
Instrumenter.new(target.key?(key) ? target[key] : nil)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# @api private
|
|
129
|
+
# @since 0.1.0
|
|
130
|
+
def build_store
|
|
131
|
+
store = config.store
|
|
132
|
+
if store.is_a?(Symbol) || store.is_a?(String)
|
|
133
|
+
factory = BUILT_IN_STORES.fetch(store.to_sym) do
|
|
134
|
+
raise ConfigurationError,
|
|
135
|
+
"unknown store #{store.inspect}; use #{BUILT_IN_STORES.keys.inspect}, a store object, " \
|
|
136
|
+
"or a factory such as Mankai::Cache::SQL.store(...)"
|
|
137
|
+
end
|
|
138
|
+
return factory.call(**store_options(store.to_sym))
|
|
139
|
+
end
|
|
140
|
+
return store if store.respond_to?(:read) && store.respond_to?(:write)
|
|
141
|
+
return store.call(target) if store.respond_to?(:call)
|
|
142
|
+
|
|
143
|
+
raise ConfigurationError, "store must be a name, a store (responds to read and write) or a factory " \
|
|
144
|
+
"(responds to call)"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# @api private
|
|
148
|
+
# @since 0.1.0
|
|
149
|
+
def store_options(name)
|
|
150
|
+
options = config.store_options
|
|
151
|
+
return options unless name == :file && !options.key?(:path)
|
|
152
|
+
|
|
153
|
+
unless defined?(::Hanami.app?) && ::Hanami.app?
|
|
154
|
+
raise ConfigurationError, "the :file store needs a path: set store_options = {path: ...}"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
options.merge(path: ::Hanami.app.root.join("tmp", "cache"))
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
class Cache
|
|
7
|
+
module Serializers
|
|
8
|
+
# @api public
|
|
9
|
+
# @since 0.1.0
|
|
10
|
+
module JSON
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# @api public
|
|
14
|
+
# @since 0.1.0
|
|
15
|
+
def dump(value)
|
|
16
|
+
::JSON.generate(value)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# @api public
|
|
20
|
+
# @since 0.1.0
|
|
21
|
+
def load(data)
|
|
22
|
+
::JSON.parse(data.dup.force_encoding(Encoding::UTF_8))
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
class Cache
|
|
5
|
+
module Serializers
|
|
6
|
+
# @api public
|
|
7
|
+
# @since 0.1.0
|
|
8
|
+
module Marshal
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
# @api public
|
|
12
|
+
# @since 0.1.0
|
|
13
|
+
def dump(value)
|
|
14
|
+
::Marshal.dump(value)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @api public
|
|
18
|
+
# @since 0.1.0
|
|
19
|
+
def load(data)
|
|
20
|
+
::Marshal.load(data) # rubocop:disable Security/MarshalLoad
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
class Cache
|
|
7
|
+
# How values become bytes.
|
|
8
|
+
#
|
|
9
|
+
# :marshal (the default) caches Ruby objects whose classes are named constants, but not ROM
|
|
10
|
+
# structs, Procs or IO (dumping those raises SerializationError: cache `struct.to_h`
|
|
11
|
+
# instead). Whoever can write to the store can make the app load arbitrary objects: keep the
|
|
12
|
+
# store private to the app. :json is safe to load from anywhere, but only round-trips plain
|
|
13
|
+
# data (Hashes come back with String keys).
|
|
14
|
+
#
|
|
15
|
+
# Any object with `dump(value) -> String` and `load(string) -> value` works too.
|
|
16
|
+
#
|
|
17
|
+
# @api public
|
|
18
|
+
# @since 0.1.0
|
|
19
|
+
module Serializers
|
|
20
|
+
# @api private
|
|
21
|
+
# @since 0.1.0
|
|
22
|
+
BUILT_IN = {marshal: "Marshal", json: "JSON"}.freeze
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# @api private
|
|
27
|
+
# @since 0.1.0
|
|
28
|
+
def resolve(serializer)
|
|
29
|
+
return serializer if serializer.respond_to?(:dump) && serializer.respond_to?(:load)
|
|
30
|
+
|
|
31
|
+
name = BUILT_IN.fetch(serializer.to_s.to_sym) do
|
|
32
|
+
raise ConfigurationError,
|
|
33
|
+
"unknown serializer #{serializer.inspect}; use #{BUILT_IN.keys.inspect} or an object with dump and load"
|
|
34
|
+
end
|
|
35
|
+
const_get(name, false)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
class Cache
|
|
5
|
+
# The contract for stores. A store keeps encoded entries (binary strings) under string keys;
|
|
6
|
+
# the cache does everything else (keys, encoding, expiry checks, versions).
|
|
7
|
+
#
|
|
8
|
+
# Every store implements:
|
|
9
|
+
#
|
|
10
|
+
# read(key) -> String or nil
|
|
11
|
+
# write(key, data, expires_at:) -> true; expires_at is seconds since the epoch, or nil.
|
|
12
|
+
# Stores may use it to evict, but need not: the cache
|
|
13
|
+
# checks expiry when it reads.
|
|
14
|
+
# delete(key) -> whether there was an entry
|
|
15
|
+
# update(key) { |data| ... } -> atomically yields the current data (or nil) and
|
|
16
|
+
# writes the [data, expires_at] the block returns
|
|
17
|
+
# clear -> deletes every entry
|
|
18
|
+
# cleanup(now) -> deletes entries expired at `now` (seconds since the
|
|
19
|
+
# epoch), where the store keeps them until asked
|
|
20
|
+
#
|
|
21
|
+
# Including this module gives one-at-a-time versions of read_multi, write_multi and
|
|
22
|
+
# delete_multi, and a close that does nothing; override them where the store can do better.
|
|
23
|
+
#
|
|
24
|
+
# @api public
|
|
25
|
+
# @since 0.1.0
|
|
26
|
+
module Store
|
|
27
|
+
# @return [Hash{String => String}] data by key, for the keys found
|
|
28
|
+
#
|
|
29
|
+
# @api public
|
|
30
|
+
# @since 0.1.0
|
|
31
|
+
def read_multi(keys)
|
|
32
|
+
keys.each_with_object({}) do |key, found|
|
|
33
|
+
data = read(key)
|
|
34
|
+
found[key] = data if data
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @param entries [Hash{String => String}] data by key
|
|
39
|
+
#
|
|
40
|
+
# @api public
|
|
41
|
+
# @since 0.1.0
|
|
42
|
+
def write_multi(entries, expires_at:)
|
|
43
|
+
entries.each { |key, data| write(key, data, expires_at: expires_at) }
|
|
44
|
+
true
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @return [Integer] how many entries were deleted
|
|
48
|
+
#
|
|
49
|
+
# @api public
|
|
50
|
+
# @since 0.1.0
|
|
51
|
+
def delete_multi(keys)
|
|
52
|
+
keys.count { |key| delete(key) }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @api public
|
|
56
|
+
# @since 0.1.0
|
|
57
|
+
def close
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
|
|
7
|
+
module Mankai
|
|
8
|
+
class Cache
|
|
9
|
+
module Stores
|
|
10
|
+
# Keeps one file per entry under a directory, shared by every process on the machine and
|
|
11
|
+
# kept across restarts.
|
|
12
|
+
#
|
|
13
|
+
# A key's file is named by its SHA-256 digest, in two levels of subdirectories so no
|
|
14
|
+
# directory grows huge: `<path>/ab/cd/abcd...`. Each file starts with the entry's expiry,
|
|
15
|
+
# so #cleanup can delete expired files without decoding them.
|
|
16
|
+
#
|
|
17
|
+
# Writes go to a temporary file that is then renamed, so readers never see half an entry.
|
|
18
|
+
# #update (used by increment) holds a lock file next to the entry while it works.
|
|
19
|
+
#
|
|
20
|
+
# Nothing is deleted until it is overwritten or #cleanup runs: run Cache#cleanup from a
|
|
21
|
+
# recurring job if keys come and go.
|
|
22
|
+
#
|
|
23
|
+
# @api public
|
|
24
|
+
# @since 0.1.0
|
|
25
|
+
class File
|
|
26
|
+
include Store
|
|
27
|
+
|
|
28
|
+
# @api private
|
|
29
|
+
# @since 0.1.0
|
|
30
|
+
EXPIRY = "G"
|
|
31
|
+
# @api private
|
|
32
|
+
# @since 0.1.0
|
|
33
|
+
EXPIRY_SIZE = 8
|
|
34
|
+
# @api private
|
|
35
|
+
# @since 0.1.0
|
|
36
|
+
ENTRY_NAME = /\A\h{64}\z/
|
|
37
|
+
# @api private
|
|
38
|
+
# @since 0.1.0
|
|
39
|
+
LEVEL_NAME = /\A\h{2}\z/
|
|
40
|
+
# Temporary and lock files older than this are left over from a crash.
|
|
41
|
+
#
|
|
42
|
+
# @api private
|
|
43
|
+
# @since 0.1.0
|
|
44
|
+
LEFTOVER_AGE = 3600
|
|
45
|
+
|
|
46
|
+
# @api public
|
|
47
|
+
# @since 0.1.0
|
|
48
|
+
attr_reader :path
|
|
49
|
+
|
|
50
|
+
# @param path [String, Pathname] the directory; created when needed
|
|
51
|
+
#
|
|
52
|
+
# @api public
|
|
53
|
+
# @since 0.1.0
|
|
54
|
+
def initialize(path:)
|
|
55
|
+
@path = ::File.expand_path(path.to_s)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# @api public
|
|
59
|
+
# @since 0.1.0
|
|
60
|
+
def read(key)
|
|
61
|
+
data = ::File.binread(entry_path(key))
|
|
62
|
+
data.byteslice(EXPIRY_SIZE..) if data.bytesize >= EXPIRY_SIZE
|
|
63
|
+
rescue Errno::ENOENT
|
|
64
|
+
nil
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @api public
|
|
68
|
+
# @since 0.1.0
|
|
69
|
+
def write(key, data, expires_at:)
|
|
70
|
+
file = entry_path(key)
|
|
71
|
+
FileUtils.mkdir_p(::File.dirname(file))
|
|
72
|
+
atomic_write(file, [expires_at || 0.0].pack(EXPIRY) + data.b)
|
|
73
|
+
true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @api public
|
|
77
|
+
# @since 0.1.0
|
|
78
|
+
def delete(key)
|
|
79
|
+
::File.delete(entry_path(key))
|
|
80
|
+
true
|
|
81
|
+
rescue Errno::ENOENT
|
|
82
|
+
false
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @api public
|
|
86
|
+
# @since 0.1.0
|
|
87
|
+
def update(key)
|
|
88
|
+
file = entry_path(key)
|
|
89
|
+
FileUtils.mkdir_p(::File.dirname(file))
|
|
90
|
+
::File.open("#{file}.lock", ::File::RDWR | ::File::CREAT, 0o644) do |lock|
|
|
91
|
+
lock.flock(::File::LOCK_EX)
|
|
92
|
+
data, expires_at = yield(read(key))
|
|
93
|
+
write(key, data, expires_at: expires_at)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Deletes every entry under the directory (only the store's own subdirectories).
|
|
98
|
+
#
|
|
99
|
+
# @api public
|
|
100
|
+
# @since 0.1.0
|
|
101
|
+
def clear
|
|
102
|
+
levels.each { |dir| FileUtils.rm_rf(dir) }
|
|
103
|
+
true
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Deletes expired entries, and temporary and lock files left over from crashes.
|
|
107
|
+
#
|
|
108
|
+
# @return [Integer] how many entries were deleted
|
|
109
|
+
#
|
|
110
|
+
# @api public
|
|
111
|
+
# @since 0.1.0
|
|
112
|
+
def cleanup(now)
|
|
113
|
+
deleted = 0
|
|
114
|
+
levels.each do |dir|
|
|
115
|
+
Dir.glob(::File.join(dir, "*", "*")).each do |file|
|
|
116
|
+
name = ::File.basename(file)
|
|
117
|
+
if name.match?(ENTRY_NAME)
|
|
118
|
+
deleted += 1 if expired?(file, now) && remove(file)
|
|
119
|
+
elsif leftover?(file, now)
|
|
120
|
+
remove(file)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
deleted
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
# @api private
|
|
130
|
+
# @since 0.1.0
|
|
131
|
+
def entry_path(key)
|
|
132
|
+
digest = Digest::SHA256.hexdigest(key)
|
|
133
|
+
::File.join(path, digest[0, 2], digest[2, 2], digest)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# @api private
|
|
137
|
+
# @since 0.1.0
|
|
138
|
+
def atomic_write(file, contents)
|
|
139
|
+
temp = "#{file}.#{SecureRandom.hex(6)}.tmp"
|
|
140
|
+
::File.binwrite(temp, contents)
|
|
141
|
+
::File.rename(temp, file)
|
|
142
|
+
ensure
|
|
143
|
+
::File.delete(temp) if temp && ::File.exist?(temp)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# @api private
|
|
147
|
+
# @since 0.1.0
|
|
148
|
+
def levels
|
|
149
|
+
return [] unless Dir.exist?(path)
|
|
150
|
+
|
|
151
|
+
Dir.children(path).select { |name| name.match?(LEVEL_NAME) }.map { |name| ::File.join(path, name) }
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# @api private
|
|
155
|
+
# @since 0.1.0
|
|
156
|
+
def expired?(file, now)
|
|
157
|
+
header = ::File.binread(file, EXPIRY_SIZE)
|
|
158
|
+
return false unless header&.bytesize == EXPIRY_SIZE
|
|
159
|
+
|
|
160
|
+
expires_at = header.unpack1(EXPIRY)
|
|
161
|
+
expires_at.positive? && expires_at <= now
|
|
162
|
+
rescue Errno::ENOENT
|
|
163
|
+
false
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# A temporary file, or a lock file whose entry is gone, untouched for an hour.
|
|
167
|
+
#
|
|
168
|
+
# @api private
|
|
169
|
+
# @since 0.1.0
|
|
170
|
+
def leftover?(file, now)
|
|
171
|
+
return false if file.end_with?(".lock") && ::File.exist?(file.delete_suffix(".lock"))
|
|
172
|
+
|
|
173
|
+
::File.mtime(file).to_f < now - LEFTOVER_AGE
|
|
174
|
+
rescue Errno::ENOENT
|
|
175
|
+
false
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# @api private
|
|
179
|
+
# @since 0.1.0
|
|
180
|
+
def remove(file)
|
|
181
|
+
::File.delete(file)
|
|
182
|
+
true
|
|
183
|
+
rescue Errno::ENOENT
|
|
184
|
+
false
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "monitor"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
class Cache
|
|
7
|
+
module Stores
|
|
8
|
+
# Keeps entries in this process's memory, up to a size limit.
|
|
9
|
+
#
|
|
10
|
+
# When the entries outgrow `size` bytes, the least recently used ones are evicted until they
|
|
11
|
+
# fill three quarters of it. Entries are kept encoded, so changing a value you read never
|
|
12
|
+
# changes the cached copy.
|
|
13
|
+
#
|
|
14
|
+
# Each process has its own copy, so Puma workers do not share entries. Good for
|
|
15
|
+
# development, tests and single-process apps.
|
|
16
|
+
#
|
|
17
|
+
# @api public
|
|
18
|
+
# @since 0.1.0
|
|
19
|
+
class Memory
|
|
20
|
+
include Store
|
|
21
|
+
|
|
22
|
+
# A rough per-entry cost, on top of the key and data, for the hash slot and arrays.
|
|
23
|
+
#
|
|
24
|
+
# @api private
|
|
25
|
+
# @since 0.1.0
|
|
26
|
+
ENTRY_OVERHEAD = 120
|
|
27
|
+
|
|
28
|
+
# @api public
|
|
29
|
+
# @since 0.1.0
|
|
30
|
+
attr_reader :size, :bytes
|
|
31
|
+
|
|
32
|
+
# @param size [Integer] the most bytes to keep (32 MB by default)
|
|
33
|
+
#
|
|
34
|
+
# @api public
|
|
35
|
+
# @since 0.1.0
|
|
36
|
+
def initialize(size: 32 * 1024 * 1024)
|
|
37
|
+
@size = size
|
|
38
|
+
@entries = {}
|
|
39
|
+
@bytes = 0
|
|
40
|
+
@monitor = Monitor.new
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @api public
|
|
44
|
+
# @since 0.1.0
|
|
45
|
+
def read(key)
|
|
46
|
+
@monitor.synchronize do
|
|
47
|
+
item = @entries.delete(key)
|
|
48
|
+
next unless item
|
|
49
|
+
|
|
50
|
+
# Reinserting moves the entry to the end: the hash is ordered from least recently used.
|
|
51
|
+
@entries[key] = item
|
|
52
|
+
item.first
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @api public
|
|
57
|
+
# @since 0.1.0
|
|
58
|
+
def write(key, data, expires_at:)
|
|
59
|
+
@monitor.synchronize do
|
|
60
|
+
put(key, data, expires_at)
|
|
61
|
+
prune if @bytes > size
|
|
62
|
+
end
|
|
63
|
+
true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @api public
|
|
67
|
+
# @since 0.1.0
|
|
68
|
+
def delete(key)
|
|
69
|
+
@monitor.synchronize { !remove(key).nil? }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @api public
|
|
73
|
+
# @since 0.1.0
|
|
74
|
+
def update(key)
|
|
75
|
+
@monitor.synchronize do
|
|
76
|
+
data, expires_at = yield(read(key))
|
|
77
|
+
write(key, data, expires_at: expires_at)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# @api public
|
|
82
|
+
# @since 0.1.0
|
|
83
|
+
def clear
|
|
84
|
+
@monitor.synchronize do
|
|
85
|
+
@entries.clear
|
|
86
|
+
@bytes = 0
|
|
87
|
+
end
|
|
88
|
+
true
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# @api public
|
|
92
|
+
# @since 0.1.0
|
|
93
|
+
def cleanup(now)
|
|
94
|
+
@monitor.synchronize do
|
|
95
|
+
expired = @entries.select { |_, (_, expires_at)| expires_at && expires_at <= now }.keys
|
|
96
|
+
expired.each { |key| remove(key) }
|
|
97
|
+
expired.size
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @return [Integer] how many entries are kept
|
|
102
|
+
#
|
|
103
|
+
# @api public
|
|
104
|
+
# @since 0.1.0
|
|
105
|
+
def count
|
|
106
|
+
@monitor.synchronize { @entries.size }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
# @api private
|
|
112
|
+
# @since 0.1.0
|
|
113
|
+
def put(key, data, expires_at)
|
|
114
|
+
remove(key)
|
|
115
|
+
key = key.dup.freeze
|
|
116
|
+
@entries[key] = [data.dup.freeze, expires_at]
|
|
117
|
+
@bytes += cost(key, data)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# @api private
|
|
121
|
+
# @since 0.1.0
|
|
122
|
+
def remove(key)
|
|
123
|
+
item = @entries.delete(key)
|
|
124
|
+
@bytes -= cost(key, item.first) if item
|
|
125
|
+
item
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Evicts the least recently used entries until three quarters of `size` are used.
|
|
129
|
+
#
|
|
130
|
+
# @api private
|
|
131
|
+
# @since 0.1.0
|
|
132
|
+
def prune
|
|
133
|
+
target = size * 3 / 4
|
|
134
|
+
remove(@entries.first.first) while @bytes > target && @entries.any?
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# @api private
|
|
138
|
+
# @since 0.1.0
|
|
139
|
+
def cost(key, data)
|
|
140
|
+
key.bytesize + data.bytesize + ENTRY_OVERHEAD
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|