async_cache 0.0.1 → 0.0.2
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/examples/examples_controller.rb +42 -0
- data/lib/async_cache/store.rb +3 -1
- data/lib/async_cache/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d672798f9a1aac8ba14134c46379e28dd0eb1de4
|
4
|
+
data.tar.gz: c590ed218b9717d22a02e5e4583f8210e38ea9ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aaf71d240d334648fc1816fa060ca35eb9b497fc02e03b81c59666f22668feb6dbfdcb680263e2ad4a41fda4ed0d8933612e3a946d7756cb1443753158f228e8
|
7
|
+
data.tar.gz: 28bb134215e14a0fe25723ab0ecee482db0eb5147bcdc301e8e3e63356daaa854710f00ade60a08c0c244d206bd3a1e22ac6c98cc155c3c16de8c2dc2260e2aa
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -33,6 +33,8 @@ async_cache.fetch(key, version, arguments: [id]) do |id|
|
|
33
33
|
end
|
34
34
|
```
|
35
35
|
|
36
|
+
For additional examples see the [`examples`](examples/) folder.
|
37
|
+
|
36
38
|
## Strategy
|
37
39
|
|
38
40
|
Async-cache follows a straightforward strategy for determining which action to take when a cache entry is fetched:
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# This is an example of how to use AsyncCache in a Rails API controller; it
|
2
|
+
# is loosely based on patterns currently in use in production at Everlane.
|
3
|
+
|
4
|
+
class ExamplesController < ApplicationController
|
5
|
+
def index
|
6
|
+
locator = 'examples'
|
7
|
+
version = Example.maximum :updated_at
|
8
|
+
options = {
|
9
|
+
expires_in: 1.week,
|
10
|
+
# Force it to synchronously regenerate for admin users so that they
|
11
|
+
# can see their changes reflected immediately
|
12
|
+
synchronous_regen: current_user.admin?
|
13
|
+
}
|
14
|
+
|
15
|
+
json = async_cache.fetch(locator, version, options) do
|
16
|
+
ExamplesPresenter.new.to_json
|
17
|
+
end
|
18
|
+
|
19
|
+
self.response_body = json
|
20
|
+
self.content_type = 'application/json'
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def async_cache
|
26
|
+
Rails.application.config.async_cache
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# We use presenters at Everlane as a sort of view-model. Controllers handle
|
31
|
+
# application logic such as writing to the database or redirecting clients
|
32
|
+
# while presenters handle loading data for the views and rendering views.
|
33
|
+
|
34
|
+
class ExamplesPresenter < Presenter
|
35
|
+
def initialize
|
36
|
+
@examples = Example.all
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_json
|
40
|
+
@examples.map(&:as_json).to_json
|
41
|
+
end
|
42
|
+
end
|
data/lib/async_cache/store.rb
CHANGED
@@ -77,9 +77,11 @@ module AsyncCache
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def generate_and_cache(key:, version:, expires_in:, block:, arguments:)
|
80
|
+
block_source = block.to_source
|
81
|
+
|
80
82
|
# Mimic the destruction-of-scope behavior of the worker in development
|
81
83
|
# so it will *fail* for developers if they try to depend upon scope
|
82
|
-
block = eval(
|
84
|
+
block = eval(block_source)
|
83
85
|
|
84
86
|
data = block.call(*arguments)
|
85
87
|
|
data/lib/async_cache/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Derewecki
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-12-
|
12
|
+
date: 2015-12-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sourcify
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- LICENSE
|
68
68
|
- README.md
|
69
69
|
- async_cache.gemspec
|
70
|
+
- examples/examples_controller.rb
|
70
71
|
- lib/async_cache.rb
|
71
72
|
- lib/async_cache/store.rb
|
72
73
|
- lib/async_cache/version.rb
|