cached_at 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 +4 -4
- data/README.md +35 -2
- data/cached_at.gemspec +1 -1
- data/lib/cached_at.rb +7 -0
- data/lib/cached_at/version.rb +1 -1
- data/test/cached_at_test.rb +11 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d073e4785a832e81ac169d68ada7c955ca1c8e8
|
4
|
+
data.tar.gz: 9b8b06f0c202052174deee91e542114f76eafed9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27194ed3a1eecbc22ecb0167c69710a77156791ee73d7318af4314aeec208b72447a4907efc2717ea5a3d0bab6ed034c3c8426b6dfb702e3fe52ffac150ebd5e
|
7
|
+
data.tar.gz: 7cda8cb407d51f94303ac1020b0678f4d5df6fc2cc46a32daea14b25423458d8c40d6f91e7a216c64e39fb59e4f3b432cd45a606abdaf595cffcbddedcedfad9
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[![Build Status](https://api.travis-ci.org/delwyn/cached_at.png?branch=master)](http://travis-ci.org/delwyn/cached_at)
|
4
4
|
[![Code Climate](https://codeclimate.com/github/delwyn/cached_at.png)](https://codeclimate.com/github/delwyn/cached_at)
|
5
5
|
|
6
|
-
|
6
|
+
Use cached_at for ActiveRecord cache key instead of updated at
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -20,12 +20,45 @@ Or install it yourself as:
|
|
20
20
|
$ gem install cached_at
|
21
21
|
|
22
22
|
## Usage
|
23
|
-
Too add to any class, just include CachedAt
|
23
|
+
Too add to any class, just include CachedAt
|
24
24
|
|
25
25
|
class User
|
26
26
|
include CachedAt
|
27
27
|
end
|
28
28
|
|
29
|
+
You will need to add the cached_at column to the table
|
30
|
+
|
31
|
+
rails generate migration add_cached_at_to_users cached_at:datetime
|
32
|
+
|
33
|
+
The cache_at column will be update when a record is created or updated.
|
34
|
+
|
35
|
+
user = User.create # sets the cached_at
|
36
|
+
user.save # updates cached_at if another attribute on the record has changed
|
37
|
+
|
38
|
+
When a records associations are created/updated, the cached_at column will be updated but the updated_at will not change.
|
39
|
+
|
40
|
+
class Post
|
41
|
+
belongs_to :user, touch: true
|
42
|
+
end
|
43
|
+
|
44
|
+
post = Post.create user: user # updates the user cached_at
|
45
|
+
|
46
|
+
Gem also provides class method `cache_key` for your model, that
|
47
|
+
will generate cache key string from `maximum(:cached_at)`
|
48
|
+
|
49
|
+
Post.cache_key
|
50
|
+
# SELECT MAX(`posts`.`cached_at`) AS max_id FROM `posts`
|
51
|
+
# => "Post-1377768216"
|
52
|
+
|
53
|
+
This way you can pass your model class to `Rails.cache.fetch` as an
|
54
|
+
argument to monitor when any record was changed.
|
55
|
+
|
56
|
+
Rails.cache.fetch [Post, 'last_posts'] do
|
57
|
+
Post.last_posts
|
58
|
+
end
|
59
|
+
# => Cache fetch_hit: Post-1377768216/last_posts
|
60
|
+
|
61
|
+
|
29
62
|
## Contributing
|
30
63
|
|
31
64
|
1. Fork it
|
data/cached_at.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["delwyn.d@gmail.com"]
|
11
11
|
spec.description = %q{Use cached_at for ActiveRecord cache key}
|
12
12
|
spec.summary = %q{Use cached_at for ActiveRecord cache key instead of updated at}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/delwyn/cached_at"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/cached_at.rb
CHANGED
@@ -4,6 +4,7 @@ module CachedAt
|
|
4
4
|
def self.included(klass)
|
5
5
|
klass.instance_eval do
|
6
6
|
before_save :_set_cached_at
|
7
|
+
klass.extend ClassMethods
|
7
8
|
end
|
8
9
|
end
|
9
10
|
|
@@ -23,6 +24,12 @@ module CachedAt
|
|
23
24
|
update_column :cached_at, current_time_from_proper_timezone
|
24
25
|
end
|
25
26
|
|
27
|
+
module ClassMethods
|
28
|
+
def cache_key
|
29
|
+
"#{model_name}-#{maximum(:cached_at).to_i}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
26
33
|
private
|
27
34
|
def _set_cached_at
|
28
35
|
self.cached_at = current_time_from_proper_timezone if new_record? || self.changed?
|
data/lib/cached_at/version.rb
CHANGED
data/test/cached_at_test.rb
CHANGED
@@ -94,4 +94,15 @@ describe 'Cached At' do
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
97
|
+
|
98
|
+
describe 'Model.cache_key' do
|
99
|
+
subject { User.cache_key }
|
100
|
+
|
101
|
+
it { subject.must_equal "User-#{User.maximum(:cached_at).to_i}" }
|
102
|
+
|
103
|
+
context 'when no record exist' do
|
104
|
+
before { User.destroy_all }
|
105
|
+
it { subject.must_equal "User-0" }
|
106
|
+
end
|
107
|
+
end
|
97
108
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cached_at
|
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
|
- Delwyn de Villiers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,7 +97,7 @@ files:
|
|
97
97
|
- lib/cached_at/version.rb
|
98
98
|
- test/cached_at_test.rb
|
99
99
|
- test/test_helper.rb
|
100
|
-
homepage:
|
100
|
+
homepage: https://github.com/delwyn/cached_at
|
101
101
|
licenses:
|
102
102
|
- MIT
|
103
103
|
metadata: {}
|
@@ -124,3 +124,4 @@ summary: Use cached_at for ActiveRecord cache key instead of updated at
|
|
124
124
|
test_files:
|
125
125
|
- test/cached_at_test.rb
|
126
126
|
- test/test_helper.rb
|
127
|
+
has_rdoc:
|