rails-cache-extended 0.0.1
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 +15 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/lib/rails-cache-extended.rb +3 -0
- data/lib/rails/cache/extended.rb +10 -0
- data/lib/rails/cache/logging.rb +34 -0
- data/lib/rails/cache/version.rb +3 -0
- data/spec/active_support_cache_log_subscriber_spec.rb +83 -0
- data/spec/cache_active_record_base_spec.rb +11 -0
- data/spec/spec_helper.rb +9 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDA5YzJjYjBhZjFmOGIyZmNhNmM2YzM2YWE2ZjcyMWFiMTQ0YzI2NA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZjNhMTgxY2UzYTA3MzdlNjhkZjlmNmFmMTMzMjU2M2YyZWRkNzJiYw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MTRkYzMzZGZmZWZlYWQ4ODJiZmI1YzQ3ZGQyZDYxNWE2ODQ0ZTkyNGQyNjY2
|
10
|
+
NjI1MjhiY2Q2NzQyMmEzOGQ3NjY4MTcxYmZlZWMyMWU5MGNlN2JmMTE5YWEx
|
11
|
+
YWYwN2IxMjFjMGMzMmQ0Y2M2Nzk5OTUxNDQ5YWZmNTYyMzY1YTg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZWI5N2ViNjdjZDUzNThhNTgyNDU3MzNkMDM1YmZmMWMzMThiYWNlNTc0ZDA5
|
14
|
+
OTc3ZmY2ZTEzNDFjNTdjZTI0MGZkNzE3MzNlMzY1NjkwZTZhNDdlYWFhNzhi
|
15
|
+
ZDU5NTBlMzY2ODA4NjIxMjc2MDQxZjYwOGUwNzc1ZjlhNjkzN2U=
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 telzamek
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Rails::Cache::Extended
|
2
|
+
|
3
|
+
This allows to generate an auto-expiring cache key for a collection of records
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rails-cache-extended'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rails-cache-extended
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rails/cache/version'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class Base
|
5
|
+
def self.cache_key
|
6
|
+
Digest::MD5.hexdigest "#{scoped.maximum(:updated_at).try(:to_i)}-#{scoped.count}"
|
7
|
+
#scoped.select("md5(COALESCE(date_part('epoch', MAX(updated_at))::char, '') || '-' || COUNT(*)) AS collection_cache_key").first.collection_cache_key
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
unless Rails.env.production?
|
3
|
+
ActiveSupport::Cache::Store.instrument = true
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
class CacheLogSubscriber < LogSubscriber
|
7
|
+
def cache_read(event)
|
8
|
+
return if event.payload[:super_operation] == :fetch
|
9
|
+
|
10
|
+
message_string = event.payload[:hit] ? "hit" : "miss"
|
11
|
+
message = " cache #{message_string}: #{event.payload[:key]}"
|
12
|
+
message << " (%.1fms)" % event.duration
|
13
|
+
|
14
|
+
event.payload[:hit] ? logger.debug(message) : logger.info(message)
|
15
|
+
end
|
16
|
+
|
17
|
+
def cache_fetch_hit(event)
|
18
|
+
message = " cache hit: #{event.payload[:key]}"
|
19
|
+
message << " (%.1fms)" % event.duration
|
20
|
+
|
21
|
+
logger.debug message
|
22
|
+
end
|
23
|
+
|
24
|
+
def cache_write(event)
|
25
|
+
message = " cache write: #{event.payload[:key]}"
|
26
|
+
message << " (%.1fms)" % event.duration
|
27
|
+
|
28
|
+
logger.info message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveSupport::CacheLogSubscriber.attach_to :active_support
|
34
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rails/cache/logging'
|
3
|
+
|
4
|
+
describe ActiveSupport::CacheLogSubscriber do
|
5
|
+
let!(:log_subscriber) { ActiveSupport::CacheLogSubscriber.new }
|
6
|
+
|
7
|
+
describe '.cache_read' do
|
8
|
+
it 'returns a valid response' do
|
9
|
+
event = double(payload: {super_operation: :test, hit: true}, duration: 2)
|
10
|
+
logger = double(debug: 1, info: 2)
|
11
|
+
|
12
|
+
ActiveSupport::CacheLogSubscriber.stub(:logger).and_return(logger)
|
13
|
+
|
14
|
+
expect(log_subscriber.cache_read(event)).to eq(1)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns nil when super_operation == fetch' do
|
18
|
+
event = double(payload: {super_operation: :fetch})
|
19
|
+
|
20
|
+
expect(log_subscriber.cache_read(event)).to be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'logger method call' do
|
24
|
+
let!(:logger) { double(debug: 1, info: 2) }
|
25
|
+
|
26
|
+
before do
|
27
|
+
ActiveSupport::CacheLogSubscriber.stub(:logger).and_return(logger)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should call debug on logger' do
|
31
|
+
event = double(payload: {super_operation: :test, hit: true, key: 'key'}, duration: 2)
|
32
|
+
|
33
|
+
logger.should_receive(:debug).with(' cache hit: key (2.0ms)')
|
34
|
+
log_subscriber.cache_read(event)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should call debug on logger' do
|
38
|
+
event = double(payload: {super_operation: :test, hit: false, key: 'key2'}, duration: 2)
|
39
|
+
|
40
|
+
logger.should_receive(:info).with(' cache miss: key2 (2.0ms)')
|
41
|
+
log_subscriber.cache_read(event)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '.cache_fetch_hit' do
|
47
|
+
let!(:logger) { double(debug: 1) }
|
48
|
+
let!(:event) { double(payload: {key: 'test'}, duration: 2) }
|
49
|
+
|
50
|
+
before do
|
51
|
+
ActiveSupport::CacheLogSubscriber.stub(:logger).and_return(logger)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns a valid response' do
|
55
|
+
expect(log_subscriber.cache_fetch_hit(event)).to eq(1)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should call debug on logger' do
|
59
|
+
logger.should_receive(:debug).with(' cache hit: test (2.0ms)')
|
60
|
+
log_subscriber.cache_fetch_hit(event)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '.cache_write' do
|
65
|
+
let!(:logger) { double(info: 1) }
|
66
|
+
let!(:event) { double(payload: {key: 'test'}, duration: 2) }
|
67
|
+
|
68
|
+
before do
|
69
|
+
ActiveSupport::CacheLogSubscriber.stub(:logger).and_return(logger)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'returns a valid response' do
|
73
|
+
expect(log_subscriber.cache_write(event)).to eq(1)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should call info on logger' do
|
77
|
+
logger.should_receive(:info).with(' cache write: test (2.0ms)')
|
78
|
+
log_subscriber.cache_write(event)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rails/cache/extended'
|
3
|
+
|
4
|
+
describe Cache::ActiveRecord::Base do
|
5
|
+
it 'generates valid cache key' do
|
6
|
+
scope = double(maximum: '2', count: 1)
|
7
|
+
Cache::ActiveRecord::Base.stub(:scoped).and_return(scope)
|
8
|
+
expected_result = Digest::MD5.hexdigest '2-1'
|
9
|
+
expect(Cache::ActiveRecord::Base::cache_key).to eq(expected_result)
|
10
|
+
end
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-cache-extended
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thibault El Zamek, Cédric Darné, Lionel Oto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ! 'This allows to generate an auto-expiring cache key for a collection
|
70
|
+
of records '
|
71
|
+
email:
|
72
|
+
- thibault.elzamek@c4mprod.com, cedric.darne@c4mprod.com, lionel.oto@c4mprod.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- lib/rails-cache-extended.rb
|
80
|
+
- lib/rails/cache/version.rb
|
81
|
+
- lib/rails/cache/extended.rb
|
82
|
+
- lib/rails/cache/logging.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/cache_active_record_base_spec.rb
|
85
|
+
- spec/active_support_cache_log_subscriber_spec.rb
|
86
|
+
homepage: https://github.com/c4mprod/rails-cache-extended
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.1.11
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: This allows to generate an auto-expiring cache key for a collection of records
|
110
|
+
test_files:
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/cache_active_record_base_spec.rb
|
113
|
+
- spec/active_support_cache_log_subscriber_spec.rb
|