greencache 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/greencache.gemspec +1 -2
- data/lib/greencache/configuration.rb +13 -0
- data/lib/greencache/version.rb +1 -1
- data/lib/greencache.rb +46 -42
- data/spec/greencache_spec.rb +25 -24
- data/spec/spec_helper.rb +0 -1
- metadata +11 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4c83976dd73945a021cc5c23a22c16e6654771e
|
4
|
+
data.tar.gz: 60b7fedbe8db7abb295d2e67b29c5d80b0e3f32b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce7746c74b41b9b5ad7ca8441a77313d5ca6f72877d9e5ad75b98b0dec68a94cead40d0fc094d958e119e26355f56bbd5df99467632efd890ac2de0d73fc009f
|
7
|
+
data.tar.gz: af8df99f8ee8eb5d14af40058dfc4d43db1bf50cc15ccf6613c81114029b9409a1e81ea49a9722dd1163ad2e262dba288895b52b96c85dd03275cffb7abd4f18
|
data/README.md
CHANGED
data/greencache.gemspec
CHANGED
@@ -22,9 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
spec.add_development_dependency "rspec", "~> 3.0"
|
24
24
|
spec.add_development_dependency "fakeredis", "~> 0.5"
|
25
|
-
spec.add_development_dependency "pliny", "~> 0.0"
|
26
25
|
|
27
26
|
spec.add_dependency "fernet", "~> 2.1"
|
28
27
|
spec.add_dependency "redis", "~> 3.1"
|
29
|
-
spec.add_dependency "multi_json", "~> 1.
|
28
|
+
spec.add_dependency "multi_json", "~> 1.9", ">= 1.9.3"
|
30
29
|
end
|
@@ -18,5 +18,18 @@ module Greencache
|
|
18
18
|
@logger = nil
|
19
19
|
@log_prefix = "greencache"
|
20
20
|
end
|
21
|
+
|
22
|
+
def to_hash
|
23
|
+
{
|
24
|
+
cache_time: cache_time,
|
25
|
+
encrypt: encrypt,
|
26
|
+
secret: secret,
|
27
|
+
skip_cache: skip_cache,
|
28
|
+
silent: silent,
|
29
|
+
logger: logger,
|
30
|
+
log_prefix: log_prefix,
|
31
|
+
redis: redis
|
32
|
+
}
|
33
|
+
end
|
21
34
|
end
|
22
35
|
end
|
data/lib/greencache/version.rb
CHANGED
data/lib/greencache.rb
CHANGED
@@ -7,6 +7,7 @@ Fernet::Configuration.run do |config|
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module Greencache
|
10
|
+
class CacheMiss < RuntimeError; end
|
10
11
|
class << self
|
11
12
|
attr_writer :configuration
|
12
13
|
|
@@ -14,46 +15,65 @@ module Greencache
|
|
14
15
|
yield(configuration)
|
15
16
|
end
|
16
17
|
|
17
|
-
def cache(redis_key, &block)
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def cache(redis_key, config = {}, &block)
|
19
|
+
config = merge_config(config)
|
20
|
+
return block.call if config[:skip_cache] || !redis_up?
|
21
|
+
read_from_cache!(redis_key, config)
|
22
|
+
rescue CacheMiss
|
21
23
|
value = block.call
|
22
|
-
write_into_cache(redis_key, value)
|
24
|
+
write_into_cache(redis_key, value, config)
|
23
25
|
value
|
24
26
|
end
|
25
27
|
|
26
|
-
def read_from_cache(redis_key,
|
27
|
-
value = get_value(redis_key)
|
28
|
-
|
29
|
-
|
28
|
+
private def read_from_cache!(redis_key, config)
|
29
|
+
value = get_value!(redis_key, config)
|
30
|
+
log("cache.hit", redis_key, config)
|
31
|
+
value
|
32
|
+
rescue CacheMiss
|
33
|
+
log("cache.miss", redis_key, config)
|
34
|
+
raise
|
35
|
+
end
|
36
|
+
|
37
|
+
def read_from_cache
|
38
|
+
read_from_cache!(redis_key)
|
39
|
+
rescue CacheMiss
|
30
40
|
end
|
31
41
|
|
32
|
-
def write_into_cache(redis_key, value)
|
42
|
+
def write_into_cache(redis_key, value, config)
|
33
43
|
with_redis do
|
34
|
-
log("cache.write", redis_key)
|
35
|
-
set_value(redis_key, value)
|
44
|
+
log("cache.write", redis_key, config)
|
45
|
+
set_value(redis_key, value, config)
|
36
46
|
end
|
37
47
|
value
|
38
48
|
end
|
39
49
|
|
40
|
-
def get_value(key)
|
41
|
-
|
50
|
+
private def get_value!(key, config)
|
51
|
+
raise CacheMiss unless redis.exists(key)
|
52
|
+
decrypt redis.get(key), config
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_value(key, config)
|
56
|
+
get_value!(key, config)
|
57
|
+
rescue CacheMiss
|
58
|
+
end
|
59
|
+
|
60
|
+
def set_value(key, value, config)
|
61
|
+
redis.setex key, config[:cache_time], encrypt(value, config)
|
42
62
|
end
|
43
63
|
|
44
|
-
def
|
45
|
-
|
64
|
+
def merge_config(config)
|
65
|
+
configuration.to_hash.merge(config)
|
46
66
|
end
|
47
67
|
|
48
|
-
def encrypt(value)
|
49
|
-
return prep_value(value) unless encrypt
|
50
|
-
fernet.generate(secret, prep_value(value))
|
68
|
+
def encrypt(value, config)
|
69
|
+
return prep_value(value) unless config[:encrypt]
|
70
|
+
fernet.generate(config[:secret], prep_value(value))
|
51
71
|
end
|
52
72
|
|
53
|
-
def decrypt(value)
|
73
|
+
def decrypt(value, config)
|
54
74
|
return nil if value.nil?
|
55
|
-
return value unless encrypt
|
56
|
-
verifier = fernet.verifier(secret, value)
|
75
|
+
return value unless config[:encrypt]
|
76
|
+
verifier = fernet.verifier(config[:secret], value)
|
57
77
|
return MultiJson.load(verifier.message) if verifier.valid?
|
58
78
|
return nil
|
59
79
|
end
|
@@ -80,14 +100,6 @@ module Greencache
|
|
80
100
|
block.call if redis_up?
|
81
101
|
end
|
82
102
|
|
83
|
-
def cache_time
|
84
|
-
configuration.cache_time
|
85
|
-
end
|
86
|
-
|
87
|
-
def skip_cache?
|
88
|
-
configuration.skip_cache
|
89
|
-
end
|
90
|
-
|
91
103
|
def test?
|
92
104
|
ENV["RACK_ENV"] == 'test'
|
93
105
|
end
|
@@ -96,20 +108,12 @@ module Greencache
|
|
96
108
|
MultiJson.encode(value)
|
97
109
|
end
|
98
110
|
|
99
|
-
def
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
def secret
|
104
|
-
configuration.secret
|
105
|
-
end
|
106
|
-
|
107
|
-
def log(str, key)
|
108
|
-
configuration.logger.log(log_prefix(str) => 1, :key => key) unless configuration.silent
|
111
|
+
def log(str, key, config)
|
112
|
+
config[:logger].log(log_prefix(str, config) => 1, :key => key) unless config[:silent]
|
109
113
|
end
|
110
114
|
|
111
|
-
def log_prefix(str)
|
112
|
-
[
|
115
|
+
def log_prefix(str, config)
|
116
|
+
[config[:log_prefix], str].join(".")
|
113
117
|
end
|
114
118
|
|
115
119
|
def fernet
|
data/spec/greencache_spec.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'fernet'
|
3
|
+
require 'logger'
|
4
|
+
require 'multi_json'
|
3
5
|
|
4
6
|
Greencache.configure do |c|
|
5
7
|
c.redis = Redis.new
|
6
|
-
c.logger =
|
8
|
+
c.logger = Logger.new(StringIO.new)
|
7
9
|
c.silent = true
|
8
10
|
end
|
9
11
|
|
@@ -48,7 +50,7 @@ describe Greencache do
|
|
48
50
|
end
|
49
51
|
|
50
52
|
it 'uses the cached' do
|
51
|
-
expect(rc).to receive(:read_from_cache){ {} }
|
53
|
+
expect(rc).to receive(:read_from_cache!){ {} }
|
52
54
|
Dummy.run
|
53
55
|
end
|
54
56
|
|
@@ -63,49 +65,48 @@ describe Greencache do
|
|
63
65
|
expect(Dummy).to receive(:get_value){ "bar" }
|
64
66
|
Dummy.run
|
65
67
|
end
|
66
|
-
|
67
68
|
end
|
68
69
|
|
69
|
-
|
70
|
-
it 'respects cache_time' do
|
71
|
-
config.cache_time = 100
|
72
|
-
expect(rc.cache_time).to eq(100)
|
73
|
-
end
|
70
|
+
context 'merging the configuration' do
|
74
71
|
|
75
|
-
it '
|
72
|
+
it 'keeps the global config' do
|
76
73
|
config.skip_cache = true
|
77
|
-
|
74
|
+
|
75
|
+
config = rc.merge_config({})
|
76
|
+
expect(config).to be_a(Hash)
|
77
|
+
expect(config[:skip_cache]).to eql(true)
|
78
78
|
end
|
79
79
|
|
80
|
-
it '
|
81
|
-
config.
|
82
|
-
|
80
|
+
it 'overrides provided values' do
|
81
|
+
config.skip_cache = true
|
82
|
+
|
83
|
+
config = rc.merge_config({skip_cache: false})
|
84
|
+
expect(config).to be_a(Hash)
|
85
|
+
expect(config[:skip_cache]).to eql(false)
|
83
86
|
end
|
84
87
|
end
|
85
88
|
|
86
89
|
it 'can write into the cache' do
|
87
90
|
p = Proc.new { "" }
|
88
|
-
|
89
|
-
rc.
|
91
|
+
config = rc.merge_config({})
|
92
|
+
expect(rc).to receive(:set_value).with("foo", "", config)
|
93
|
+
rc.write_into_cache("foo", p.call, config)
|
90
94
|
end
|
91
95
|
|
92
|
-
it
|
96
|
+
it "can get a value that's been set" do
|
97
|
+
rc.redis.set "foo", "bar"
|
93
98
|
expect(rc.redis).to receive(:get).with("foo"){ "bar" }
|
94
|
-
expect(rc).to receive(:decrypt).with("bar")
|
95
|
-
rc.get_value("foo")
|
99
|
+
expect(rc).to receive(:decrypt).with("bar", {encrypt: true})
|
100
|
+
rc.get_value("foo", {encrypt: true})
|
96
101
|
end
|
97
102
|
|
98
103
|
it 'can set a value' do
|
99
|
-
config.cache_time = 100
|
100
|
-
config.encrypt = false
|
101
104
|
expect(rc.redis).to receive(:setex).with("foo", 100, '"bar"')
|
102
|
-
rc.set_value("foo", "bar")
|
105
|
+
rc.set_value("foo", "bar", {cache_time: 100, encrypt: false})
|
103
106
|
end
|
104
107
|
|
105
108
|
it 'encrypts' do
|
106
|
-
config.encrypt = true
|
107
|
-
config.secret = "foo"
|
108
109
|
expect(rc.fernet).to receive(:generate).with("foo", '"bar"'){ "abc" }
|
109
|
-
rc.encrypt("bar")
|
110
|
+
rc.encrypt("bar", {encrypt: true, secret: 'foo'})
|
110
111
|
end
|
111
112
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: greencache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neil Middleton
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-09-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -67,20 +67,6 @@ dependencies:
|
|
67
67
|
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0.5'
|
70
|
-
- !ruby/object:Gem::Dependency
|
71
|
-
name: pliny
|
72
|
-
requirement: !ruby/object:Gem::Requirement
|
73
|
-
requirements:
|
74
|
-
- - "~>"
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
version: '0.0'
|
77
|
-
type: :development
|
78
|
-
prerelease: false
|
79
|
-
version_requirements: !ruby/object:Gem::Requirement
|
80
|
-
requirements:
|
81
|
-
- - "~>"
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
version: '0.0'
|
84
70
|
- !ruby/object:Gem::Dependency
|
85
71
|
name: fernet
|
86
72
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,14 +101,20 @@ dependencies:
|
|
115
101
|
requirements:
|
116
102
|
- - "~>"
|
117
103
|
- !ruby/object:Gem::Version
|
118
|
-
version: '1.
|
104
|
+
version: '1.9'
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 1.9.3
|
119
108
|
type: :runtime
|
120
109
|
prerelease: false
|
121
110
|
version_requirements: !ruby/object:Gem::Requirement
|
122
111
|
requirements:
|
123
112
|
- - "~>"
|
124
113
|
- !ruby/object:Gem::Version
|
125
|
-
version: '1.
|
114
|
+
version: '1.9'
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.9.3
|
126
118
|
description: A wrapper for Redis, for caching and encryption with Fernet
|
127
119
|
email:
|
128
120
|
- neil@neilmiddleton.com
|
@@ -162,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
154
|
version: '0'
|
163
155
|
requirements: []
|
164
156
|
rubyforge_project:
|
165
|
-
rubygems_version: 2.4.5
|
157
|
+
rubygems_version: 2.4.5.1
|
166
158
|
signing_key:
|
167
159
|
specification_version: 4
|
168
160
|
summary: A gem for caching values in redis and encrypting them
|