object-cache 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b329bb499b49a3b608f4ba5a756e09a246228c28
4
- data.tar.gz: e5bf39f2fd0025bd30568d4fe87aaf29eb2e077b
3
+ metadata.gz: b3be3bd853f9ffd24ff89cff95d2762241021a1f
4
+ data.tar.gz: 48fc790b6f2a15b13d945a9fe6f0483355bc9995
5
5
  SHA512:
6
- metadata.gz: e14b55bf9cf2fa92ca9d307740ad8d31df097616a8f0642e362b4b1398ac7eef4480646df252e3abfdf154de5f9b06610406426fdcffa61bc32bef2007114a71
7
- data.tar.gz: 4ab4b813991e17132477fa97531636d8a04778eeb14ef221a85afbe0503061e85671e6b195f80bca42f6962cf1b60d297c7672a683bc28528273375721b73276
6
+ metadata.gz: 6e1cd521b1b326ab8a381b71a0c39677fc37885dd0f714c8e8db010d7cc22dd5e88067827be3c4a863bda017f847ff63f77ab5814be6de0e20d527f986b4cf2e
7
+ data.tar.gz: 12f817db636a6d87ed92dbc80885d999f0b19b11b507aab5283fcf1ee7d9941495e430b0ff47571ab2601c559a5017703dde3de97f7d8666f04b6bf97dd4f4ae
data/.rubocop.yml CHANGED
@@ -6,5 +6,8 @@ AllCops:
6
6
  Metrics/MethodLength:
7
7
  Max: 15
8
8
 
9
+ Metrics/AbcSize:
10
+ Max: 16
11
+
9
12
  Metrics/LineLength:
10
13
  Max: 100
data/README.md CHANGED
@@ -83,7 +83,14 @@ that every request after the first request uses the value from the cached
83
83
  object. After one week, the cached value becomes stale, and the first request
84
84
  after that will again store the (possibly changed) object in the cache store.
85
85
 
86
- You can easily modify the `ttl` using the keyword argument by that same name:
86
+ You can globaly set the default ttl to a different value:
87
+
88
+ ```ruby
89
+ Cache.default_ttl = 120
90
+ ```
91
+
92
+ You can easily modify the `ttl` per cached object, using the keyword argument by
93
+ that same name:
87
94
 
88
95
  ```ruby
89
96
  Cache.new(ttl: 60) { 'remember me for 60 seconds!' }
@@ -96,6 +103,11 @@ Cache.new(ttl: nil) { 'I am forever in your cache!' }
96
103
  Cache.new(ttl: 0) { 'me too!' }
97
104
  ```
98
105
 
106
+ Note that it is best to never leave a value in the backend forever. Since this
107
+ library uses file names and line numbers to store the value, a change in your
108
+ code might mean a new cache object is created after a deployment, and your old
109
+ cache object becomes orphaned, and will polute your storage forever.
110
+
99
111
  #### namespaced keys
100
112
 
101
113
  When storing the key/value object into Redis, the key name is based on the file
@@ -136,6 +148,9 @@ backend config, with a `primary` and `replicas` key:
136
148
  Cache.backend = { primary: Redis.new, replicas: [Redis.new, Redis.new] }
137
149
  ```
138
150
 
151
+ When writing the initial object to the backend, the `primary` Redis is used. On
152
+ subsequent requests, a random replica is used to retrieve the stored value.
153
+
139
154
  The above example obiously only works if the replicas receive the written data
140
155
  from the primary instance.
141
156
 
@@ -152,6 +167,15 @@ cache('hello', ttl: 60) { 'hello world' }
152
167
  Cache.new('hello', ttl: 60) { 'hello world' }
153
168
  ```
154
169
 
170
+ Since the core extension adds the `cache` method to the `Object` class, you can
171
+ also call this method directly on any instances inheriting from `Object`:
172
+
173
+ ```ruby
174
+ require 'object/cache/core_extension'
175
+
176
+ 'hello world'.cache(ttl: 60)
177
+ ```
178
+
155
179
  That's it!
156
180
 
157
181
  ## License
data/lib/object/cache.rb CHANGED
@@ -5,10 +5,11 @@ require 'object/cache/version'
5
5
 
6
6
  # Caching of objects in a Redis store
7
7
  class Cache
8
- DEFAULT_TTL = (7 * 24 * 60 * 60) # 1 week
8
+ @default_ttl = (7 * 24 * 60 * 60) # 1 week
9
9
 
10
10
  class << self
11
11
  attr_accessor :backend
12
+ attr_accessor :default_ttl
12
13
 
13
14
  # new
14
15
  #
@@ -42,7 +43,7 @@ class Cache
42
43
  # Cache.new { item } # item is only stored once, and then always
43
44
  # # retrieved, even if it is a different item
44
45
  #
45
- def new(key = nil, ttl: DEFAULT_TTL)
46
+ def new(key = nil, ttl: default_ttl)
46
47
  return yield unless replica
47
48
 
48
49
  key = Digest::SHA1.hexdigest([key, Proc.new.source_location].flatten.join)[0..5]
@@ -76,7 +77,7 @@ class Cache
76
77
  true
77
78
  end
78
79
 
79
- def update_cache(key, value, ttl: DEFAULT_TTL)
80
+ def update_cache(key, value, ttl: default_ttl)
80
81
  return unless primary && (value = Marshal.dump(value))
81
82
 
82
83
  ttl.to_i.zero? ? primary.set(key, value) : primary.setex(key, ttl.to_i, value)
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
+ require 'object/cache'
2
3
 
3
4
  # :no-doc:
4
5
  class Object
5
6
  def cache(key = nil, **options, &block)
7
+ block = -> { self } unless block_given?
8
+
6
9
  Cache.new(key, **options, &block)
7
10
  end
8
11
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Cache
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
data/test/cache_test.rb CHANGED
@@ -6,9 +6,10 @@ require 'object/cache'
6
6
  require 'minitest/autorun'
7
7
 
8
8
  # :no-doc:
9
- class CacheTest < Minitest::Test
9
+ class CacheTest < Minitest::Test # rubocop:disable Metrics/ClassLength
10
10
  def setup
11
11
  Cache.backend = MockRedis.new
12
+ Cache.default_ttl = 604_800
12
13
  end
13
14
 
14
15
  def redis
@@ -77,7 +78,13 @@ class CacheTest < Minitest::Test
77
78
 
78
79
  def test_cache_default_ttl
79
80
  Cache.new { 'hello world' }
80
- assert_equal Cache::DEFAULT_TTL, redis.ttl(redis.keys.first)
81
+ assert_equal Cache.default_ttl, redis.ttl(redis.keys.first)
82
+ end
83
+
84
+ def test_cache_custom_default_ttl
85
+ Cache.default_ttl = 60
86
+ Cache.new { 'hello world' }
87
+ assert_equal 60, redis.ttl(redis.keys.first)
81
88
  end
82
89
 
83
90
  def test_cache_with_ttl
@@ -98,6 +105,19 @@ class CacheTest < Minitest::Test
98
105
  assert Object.send(:remove_method, :cache)
99
106
  end
100
107
 
108
+ def test_core_extension_on_objects
109
+ load 'object/cache/core_extension.rb'
110
+ assert_equal 'hello world', 'hello world'.cache
111
+ assert Object.send(:remove_method, :cache)
112
+ end
113
+
114
+ def test_core_extension_on_objects_with_arguments
115
+ load 'object/cache/core_extension.rb'
116
+ 'hello world'.cache(ttl: 30)
117
+ assert_equal 30, redis.ttl(redis.keys.first)
118
+ assert Object.send(:remove_method, :cache)
119
+ end
120
+
101
121
  def test_backend_with_replicas
102
122
  Cache.backend = { primary: redis, replicas: [redis, redis] }
103
123
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean
@@ -154,4 +154,3 @@ signing_key:
154
154
  specification_version: 4
155
155
  summary: Caching of objects, using a Redis store.
156
156
  test_files: []
157
- has_rdoc: