prefatory 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/prefatory.rb +6 -13
- data/lib/prefatory/config.rb +10 -0
- data/lib/prefatory/key_generator.rb +19 -0
- data/lib/prefatory/storage/discover.rb +12 -3
- data/lib/prefatory/storage/memcached_provider.rb +20 -12
- data/lib/prefatory/storage/redis_provider.rb +16 -10
- data/lib/prefatory/storage/test_provider.rb +10 -11
- data/lib/prefatory/version.rb +1 -1
- metadata +4 -5
- data/lib/prefatory/keys.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab95dbbd20cd5b03132b0dcbfe30ccfde2fb1b2bca7deb538c4eb54982b19a7a
|
4
|
+
data.tar.gz: 7e011d86d50c43cf1e60b392e9cff5d968230793150766a0294ee063cc8cc09f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c9ae5daa2224cb673a1454f52806dcbdb8cd65c12d09e2149f206470321c280252d3881ecb956e5fa16a0e70ce5b0270e88ccc6c5e0e51d71e80ec8f6a323e1
|
7
|
+
data.tar.gz: e624b6605b4d317b147029cb44ad4c1e3ed9e6f2458a985546cd37748c463b0a719254b54d3d051aa1ce93e278e52daf084979f962adef9684312707b6ae2b1e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -50,9 +50,9 @@ Redis:
|
|
50
50
|
|
51
51
|
# default configuration:
|
52
52
|
Prefatory.configure do |config|
|
53
|
-
|
53
|
+
config.storage.provider = :redis
|
54
54
|
# accepts any additional options redis-rb gem accepts
|
55
|
-
|
55
|
+
config.storage.options = {url: ENV['REDIS_URL']||Redis.current||'redis://127.0.0.1:0'}
|
56
56
|
config.ttl =
|
57
57
|
end
|
58
58
|
|
data/lib/prefatory.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require_relative "prefatory/version"
|
2
2
|
require_relative 'prefatory/config'
|
3
3
|
require_relative 'prefatory/errors'
|
4
|
-
require_relative 'prefatory/keys'
|
5
4
|
require_relative 'prefatory/storage/discover'
|
6
5
|
|
7
6
|
module Prefatory
|
@@ -10,17 +9,14 @@ module Prefatory
|
|
10
9
|
KEY_NOT_FOUND_MSG = "Unable to find the object with key ?"
|
11
10
|
|
12
11
|
def initialize(key_prefix: nil, storage: nil,
|
13
|
-
config: Prefatory.config
|
14
|
-
marshaler: Marshal)
|
12
|
+
config: Prefatory.config)
|
15
13
|
@config = config
|
16
|
-
@storage = storage || Storage::Discover.new(@config.storage, @config.ttl
|
17
|
-
@marshaler = marshaler
|
14
|
+
@storage = storage || Storage::Discover.new(@config.storage, @config.ttl).instance
|
18
15
|
end
|
19
16
|
|
20
17
|
def find(key)
|
21
18
|
return nil unless @storage.key?(key)
|
22
|
-
|
23
|
-
@marshaler.load(value)
|
19
|
+
@storage.get(key)
|
24
20
|
end
|
25
21
|
|
26
22
|
def find!(key)
|
@@ -31,9 +27,8 @@ module Prefatory
|
|
31
27
|
|
32
28
|
def save(obj, ttl=nil)
|
33
29
|
begin
|
34
|
-
value = @marshaler.dump(obj)
|
35
30
|
key = @storage.next_key(obj)
|
36
|
-
@storage.set(key,
|
31
|
+
@storage.set(key, obj, ttl)
|
37
32
|
rescue StandardError => e
|
38
33
|
logger.info("An error occurred (#{e.inspect}) storing object: #{obj.inspect}")
|
39
34
|
key = nil
|
@@ -50,13 +45,13 @@ module Prefatory
|
|
50
45
|
|
51
46
|
def update(key, obj, ttl=nil)
|
52
47
|
return false unless @storage.key?(key)
|
53
|
-
|
54
|
-
@storage.set(key, value, ttl)
|
48
|
+
@storage.set(key, obj, ttl)
|
55
49
|
true
|
56
50
|
end
|
57
51
|
|
58
52
|
def update!(key, obj, ttl=nil)
|
59
53
|
raise Errors::NotFound, KEY_NOT_FOUND_MSG.sub('?',key) unless @storage.key?(key)
|
54
|
+
|
60
55
|
update(key, obj, ttl)
|
61
56
|
end
|
62
57
|
|
@@ -73,8 +68,6 @@ module Prefatory
|
|
73
68
|
|
74
69
|
private
|
75
70
|
|
76
|
-
include Prefatory::Keys
|
77
|
-
|
78
71
|
def logger
|
79
72
|
@config.logger
|
80
73
|
end
|
data/lib/prefatory/config.rb
CHANGED
@@ -1,12 +1,22 @@
|
|
1
1
|
require 'dry-configurable'
|
2
|
+
require_relative 'key_generator'
|
2
3
|
|
3
4
|
module Prefatory
|
4
5
|
extend Dry::Configurable
|
5
6
|
|
6
7
|
setting :logger, Logger.new(STDOUT)
|
7
8
|
setting :ttl, 86400 # in seconds - defaults to one day
|
9
|
+
setting :keys do
|
10
|
+
setting :generator, Prefatory::KeyGenerator
|
11
|
+
setting :prefix, 'prefatory'
|
12
|
+
# If the object being stored responds to :entity_primary_key and that value is not null,
|
13
|
+
# it is assumed to be a universally unique primary key for the object.
|
14
|
+
# The default key_generator will use it
|
15
|
+
setting :primary_uuid, :primary_uuid
|
16
|
+
end
|
8
17
|
setting :storage do
|
9
18
|
setting :provider # :memcached or :redis
|
10
19
|
setting :options
|
20
|
+
setting :marshaler, Marshal
|
11
21
|
end
|
12
22
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Prefatory
|
4
|
+
class KeyGenerator
|
5
|
+
def initialize(prefix = nil)
|
6
|
+
@prefix = prefix
|
7
|
+
end
|
8
|
+
|
9
|
+
def prefix(key)
|
10
|
+
[@prefix, key].compact.join(':')
|
11
|
+
end
|
12
|
+
|
13
|
+
def key(obj)
|
14
|
+
next_key = obj.send(Prefatory.config.keys.primary_uuid) if obj.respond_to?(Prefatory.config.keys.primary_uuid)
|
15
|
+
next_key = SecureRandom.hex(16) unless next_key
|
16
|
+
next_key
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,18 +1,27 @@
|
|
1
|
+
require 'prefatory/config'
|
2
|
+
require 'prefatory/errors'
|
3
|
+
|
1
4
|
module Prefatory
|
2
5
|
module Storage
|
3
6
|
class Discover
|
4
|
-
def initialize(config, ttl
|
7
|
+
def initialize(config, ttl=Prefatory.config.ttl)
|
5
8
|
@config = config
|
6
9
|
@ttl = ttl
|
7
|
-
@key_prefix = key_prefix
|
8
10
|
@provider = find_provider(config.provider)
|
9
11
|
end
|
10
12
|
|
11
13
|
def instance
|
12
14
|
require_relative "#{@provider}_provider"
|
13
|
-
Object.const_get("Prefatory::Storage::#{@provider.to_s.capitalize}Provider").new(@config.options,
|
15
|
+
Object.const_get("Prefatory::Storage::#{@provider.to_s.capitalize}Provider").new(@config.options,
|
16
|
+
@ttl,
|
17
|
+
marshaler: marshaler)
|
14
18
|
end
|
15
19
|
private
|
20
|
+
|
21
|
+
def marshaler
|
22
|
+
@config.respond_to?(:marshaler) ? @config.marshaler : Marshal
|
23
|
+
end
|
24
|
+
|
16
25
|
def find_provider(provider)
|
17
26
|
return provider if provider
|
18
27
|
return :redis if defined?(Redis)
|
@@ -3,46 +3,54 @@ require 'dalli'
|
|
3
3
|
module Prefatory
|
4
4
|
module Storage
|
5
5
|
class MemcachedProvider
|
6
|
-
include Prefatory::Keys
|
7
6
|
DEFAULT_SERVER = '127.0.0.1:11211'.freeze
|
8
|
-
DEFAULT_OPTIONS = {namespace:
|
7
|
+
DEFAULT_OPTIONS = {namespace: Prefatory.config.keys.prefix, compress: true, cache_nils: true}
|
9
8
|
|
10
|
-
def initialize(options
|
9
|
+
def initialize(options = nil, ttl = nil,
|
10
|
+
key_generator: Prefatory.config.keys.generator.new,
|
11
|
+
marshaler: Prefatory.config.storage.marshaler)
|
11
12
|
@ttl = ttl
|
12
|
-
@
|
13
|
+
@key_generator = key_generator
|
14
|
+
@marshaler = marshaler
|
13
15
|
@client = Dalli::Client.new(default_servers(options), default_options(options))
|
14
16
|
end
|
15
17
|
|
16
|
-
def set(key, value, ttl=nil)
|
17
|
-
@client.set(key, value, ttl
|
18
|
+
def set(key, value, ttl = nil)
|
19
|
+
@client.set(prefix(key), @marshaler.dump(value), ttl || @ttl)
|
18
20
|
end
|
19
21
|
|
20
22
|
def get(key)
|
21
|
-
@client.get(key)
|
23
|
+
value = @client.get(prefix(key))
|
24
|
+
value ? @marshaler.load(value) : value
|
22
25
|
end
|
23
26
|
|
24
27
|
def delete(key)
|
25
|
-
@client.delete(key)
|
28
|
+
@client.delete(prefix(key))
|
26
29
|
end
|
27
30
|
|
28
31
|
def key?(key)
|
29
|
-
v = @client.fetch(key){:does_not_exists}
|
32
|
+
v = @client.fetch(prefix(key)) {:does_not_exists}
|
30
33
|
v != :does_not_exists
|
31
34
|
end
|
32
35
|
|
33
|
-
def next_key(obj=nil)
|
34
|
-
|
36
|
+
def next_key(obj = nil)
|
37
|
+
@key_generator.key(obj)
|
35
38
|
end
|
36
39
|
|
37
40
|
private
|
38
41
|
|
42
|
+
def prefix(key)
|
43
|
+
@key_generator.prefix(key)
|
44
|
+
end
|
45
|
+
|
46
|
+
|
39
47
|
def default_servers(options)
|
40
48
|
return options[:servers] if options&.fetch(:servers) {false}
|
41
49
|
DEFAULT_SERVER
|
42
50
|
end
|
43
51
|
|
44
52
|
def default_options(options)
|
45
|
-
return options.
|
53
|
+
return options.select {|key| key != :servers} if options
|
46
54
|
DEFAULT_OPTIONS
|
47
55
|
end
|
48
56
|
end
|
@@ -3,40 +3,46 @@ require 'redis'
|
|
3
3
|
module Prefatory
|
4
4
|
module Storage
|
5
5
|
class RedisProvider
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def initialize(options=nil, ttl=Prefatory.config.ttl,
|
7
|
+
key_generator: Prefatory.config.keys.generator.new(Prefatory.config.keys.prefix),
|
8
|
+
marshaler: Prefatory.config.storage.marshaler)
|
9
9
|
options = default_settings(options)
|
10
10
|
@ttl = ttl
|
11
|
-
@
|
11
|
+
@key_generator = key_generator
|
12
|
+
@marshaler = marshaler
|
12
13
|
@client = options ? Redis.new(options) : Redis.current
|
13
14
|
end
|
14
15
|
|
15
16
|
def set(key, value, ttl=nil)
|
16
|
-
@client.set(key, value, ex: ttl||@ttl)
|
17
|
+
@client.set(prefix(key), @marshaler.dump(value), ex: ttl||@ttl)
|
17
18
|
end
|
18
19
|
|
19
20
|
def get(key)
|
20
|
-
@client.get(key)
|
21
|
+
value = @client.get(prefix(key))
|
22
|
+
value ? @marshaler.load(value) : value
|
21
23
|
end
|
22
24
|
|
23
25
|
def delete(key)
|
24
|
-
@client.del(key)
|
26
|
+
@client.del(prefix(key))
|
25
27
|
end
|
26
28
|
|
27
29
|
def key?(key)
|
28
|
-
@client.exists(key)
|
30
|
+
@client.exists(prefix(key))
|
29
31
|
end
|
30
32
|
|
31
33
|
def next_key(obj=nil)
|
32
|
-
|
34
|
+
@key_generator.key(obj)
|
33
35
|
end
|
34
36
|
|
35
37
|
private
|
36
38
|
|
39
|
+
def prefix(key)
|
40
|
+
@key_generator.prefix(key)
|
41
|
+
end
|
42
|
+
|
37
43
|
def default_settings(options)
|
38
44
|
return options if options&.fetch(:url){false} || options&.fetch(:host){false}
|
39
|
-
url = ENV['REDIS_PROVIDER'] || ENV['REDIS_URL']
|
45
|
+
url = ENV['REDIS_PROVIDER'] || ENV['REDIS_URL'] || ENV['REDIS_SERVER']
|
40
46
|
if (url)
|
41
47
|
options ||= {}
|
42
48
|
options = options.merge(url: url)
|
@@ -1,24 +1,23 @@
|
|
1
|
-
require 'dalli'
|
2
|
-
|
3
1
|
module Prefatory
|
4
2
|
module Storage
|
5
3
|
# Used by Specs. This is not safe for production (in any way!) You have been warned.
|
6
4
|
class TestProvider
|
7
|
-
include Prefatory::Keys
|
8
5
|
|
9
|
-
def initialize(
|
10
|
-
|
11
|
-
|
6
|
+
def initialize(_ttl = nil,
|
7
|
+
key_generator: Prefatory.config.keys.generator.new,
|
8
|
+
marshaler: Prefatory.config.storage.marshaler)
|
12
9
|
@hash = {}
|
13
|
-
@
|
10
|
+
@key_generator = key_generator
|
11
|
+
@marshaler = marshaler
|
14
12
|
end
|
15
13
|
|
16
|
-
def set(key, value,
|
17
|
-
@hash.store(key, value)
|
14
|
+
def set(key, value, _ttl=nil)
|
15
|
+
@hash.store(key, @marshaler.dump(value))
|
18
16
|
end
|
19
17
|
|
20
18
|
def get(key)
|
21
|
-
@hash.fetch(key){nil}
|
19
|
+
value = @hash.fetch(key){nil}
|
20
|
+
value ? @marshaler.load(value) : value
|
22
21
|
end
|
23
22
|
|
24
23
|
def key?(key)
|
@@ -30,7 +29,7 @@ module Prefatory
|
|
30
29
|
end
|
31
30
|
|
32
31
|
def next_key(obj = nil)
|
33
|
-
|
32
|
+
@key_generator.key(obj)
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
data/lib/prefatory/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prefatory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Russell Edens
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-configurable
|
@@ -132,7 +132,7 @@ files:
|
|
132
132
|
- lib/prefatory.rb
|
133
133
|
- lib/prefatory/config.rb
|
134
134
|
- lib/prefatory/errors.rb
|
135
|
-
- lib/prefatory/
|
135
|
+
- lib/prefatory/key_generator.rb
|
136
136
|
- lib/prefatory/storage/discover.rb
|
137
137
|
- lib/prefatory/storage/memcached_provider.rb
|
138
138
|
- lib/prefatory/storage/redis_provider.rb
|
@@ -158,8 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
160
|
requirements: []
|
161
|
-
|
162
|
-
rubygems_version: 2.7.6
|
161
|
+
rubygems_version: 3.0.2
|
163
162
|
signing_key:
|
164
163
|
specification_version: 4
|
165
164
|
summary: Prefatory provides storage of entities (models or values/attributes) in a
|
data/lib/prefatory/keys.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
module Prefatory
|
2
|
-
module Keys
|
3
|
-
INCR_KEY = 'prefatory'.freeze
|
4
|
-
|
5
|
-
def build_key(obj=nil,value=nil, prefix=nil)
|
6
|
-
klass = nil
|
7
|
-
klass = obj&.class == 'Class' ? obj.name.downcase : obj&.class.name.downcase if obj
|
8
|
-
"#{INCR_KEY}#{prefix ? ":#{prefix}" : nil}#{klass ? ":#{klass}" : nil}#{value ? ":#{value}" : nil}"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|