caching 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.coveralls.yml +2 -0
- data/.gitignore +17 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +10 -0
- data/caching.gemspec +26 -0
- data/lib/caching/proxy.rb +25 -0
- data/lib/caching/storage.rb +29 -0
- data/lib/caching/version.rb +3 -0
- data/lib/caching.rb +17 -0
- data/spec/caching_spec.rb +61 -0
- data/spec/coverage_helper.rb +8 -0
- data/spec/minitest_helper.rb +9 -0
- data/spec/proxy_spec.rb +59 -0
- data/spec/storage_spec.rb +47 -0
- metadata +149 -0
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Gabriel Naiman
|
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,51 @@
|
|
1
|
+
# Caching
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/caching.png)](https://rubygems.org/gems/caching)
|
4
|
+
[![Build Status](https://travis-ci.org/gabynaiman/caching.png?branch=master)](https://travis-ci.org/gabynaiman/caching)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/gabynaiman/caching/badge.png?branch=master)](https://coveralls.io/r/gabynaiman/caching?branch=master)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/gabynaiman/caching.png)](https://codeclimate.com/github/gabynaiman/caching)
|
7
|
+
[![Dependency Status](https://gemnasium.com/gabynaiman/caching.png)](https://gemnasium.com/gabynaiman/caching)
|
8
|
+
|
9
|
+
Cache methods
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'caching'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install caching
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
class Model
|
28
|
+
extend Caching
|
29
|
+
|
30
|
+
cache :slow_method, :other_slow_method
|
31
|
+
|
32
|
+
def slow_method
|
33
|
+
...
|
34
|
+
end
|
35
|
+
|
36
|
+
def other_slow_method
|
37
|
+
...
|
38
|
+
end
|
39
|
+
|
40
|
+
def fast_method
|
41
|
+
...
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/caching.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'caching/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "caching"
|
8
|
+
spec.version = Caching::VERSION
|
9
|
+
spec.authors = ["Gabriel Naiman"]
|
10
|
+
spec.email = ["gnaiman@keepcon.com"]
|
11
|
+
spec.description = 'Cache methods'
|
12
|
+
spec.summary = 'Cache methods'
|
13
|
+
spec.homepage = "https://github.com/gabynaiman/caching"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest", "~> 4.7"
|
24
|
+
spec.add_development_dependency "turn", "~> 0.9"
|
25
|
+
spec.add_development_dependency "simplecov"
|
26
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Caching
|
2
|
+
class Proxy
|
3
|
+
|
4
|
+
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
|
5
|
+
|
6
|
+
def initialize(target, *methods)
|
7
|
+
@target = target
|
8
|
+
@methods = methods.map(&:to_sym)
|
9
|
+
@storage = Storage.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(method, *args, &block)
|
13
|
+
if @methods.include? method.to_sym
|
14
|
+
@storage.fetch(method) { @target.send(method, *args, &block) }
|
15
|
+
else
|
16
|
+
@target.send(method, *args, &block)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear_cache(*methods)
|
21
|
+
@storage.clear *methods
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Caching
|
2
|
+
class Storage
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@storage = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def read(key)
|
9
|
+
@storage[key]
|
10
|
+
end
|
11
|
+
|
12
|
+
def write(key, value)
|
13
|
+
@storage[key] = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch(key)
|
17
|
+
read(key) || write(key, yield)
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear(*keys)
|
21
|
+
if keys.empty?
|
22
|
+
@storage = {}
|
23
|
+
else
|
24
|
+
keys.each { |k| @storage.delete k }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/caching.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Dir["#{File.dirname(__FILE__)}/caching/*.rb"].each { |file| require file }
|
2
|
+
|
3
|
+
module Caching
|
4
|
+
|
5
|
+
def self.extended(klass)
|
6
|
+
constructor = klass.method :new
|
7
|
+
|
8
|
+
klass.define_singleton_method :new do |*args, &block|
|
9
|
+
Proxy.new constructor.call(*args, &block), *@cached_methods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def cache(*methods)
|
14
|
+
@cached_methods = (@cached_methods ||= []) | methods.map(&:to_sym).uniq
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
describe Caching do
|
4
|
+
|
5
|
+
class Cachable
|
6
|
+
extend Caching
|
7
|
+
cache :slow_method, :other_slow_method
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@slow_method = 0
|
11
|
+
@other_slow_method = 0
|
12
|
+
@fast_method = 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def slow_method
|
16
|
+
@slow_method += 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def other_slow_method
|
20
|
+
@other_slow_method += 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def fast_method
|
24
|
+
@fast_method += 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'Cache methods' do
|
29
|
+
object = Cachable.new
|
30
|
+
|
31
|
+
3.times { object.slow_method.must_equal 1 }
|
32
|
+
3.times { object.other_slow_method.must_equal 1 }
|
33
|
+
object.fast_method.must_equal 1
|
34
|
+
object.fast_method.must_equal 2
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'Clear cache for all methods' do
|
38
|
+
object = Cachable.new
|
39
|
+
|
40
|
+
3.times { object.slow_method.must_equal 1 }
|
41
|
+
3.times { object.other_slow_method.must_equal 1 }
|
42
|
+
|
43
|
+
object.clear_cache
|
44
|
+
|
45
|
+
object.slow_method.must_equal 2
|
46
|
+
object.other_slow_method.must_equal 2
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'Clear cache for specific method' do
|
50
|
+
object = Cachable.new
|
51
|
+
|
52
|
+
3.times { object.slow_method.must_equal 1 }
|
53
|
+
3.times { object.other_slow_method.must_equal 1 }
|
54
|
+
|
55
|
+
object.clear_cache :slow_method
|
56
|
+
|
57
|
+
object.slow_method.must_equal 2
|
58
|
+
object.other_slow_method.must_equal 1
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/spec/proxy_spec.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
describe Caching::Proxy do
|
4
|
+
|
5
|
+
class Fake
|
6
|
+
attr_reader :x, :y
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@x = 0
|
10
|
+
@y = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def next_x
|
14
|
+
@x += 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def next_y
|
18
|
+
@y += 1
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'Cache specific method' do
|
23
|
+
object = Fake.new
|
24
|
+
proxy = Caching::Proxy.new object, :next_x
|
25
|
+
|
26
|
+
proxy.next_x.must_equal 1
|
27
|
+
proxy.next_x.must_equal 1
|
28
|
+
|
29
|
+
proxy.next_y.must_equal 1
|
30
|
+
proxy.next_y.must_equal 2
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'Clear cache for all methods' do
|
34
|
+
object = Fake.new
|
35
|
+
proxy = Caching::Proxy.new object, :next_x, :next_y
|
36
|
+
|
37
|
+
3.times{ proxy.next_x.must_equal 1 }
|
38
|
+
3.times{ proxy.next_y.must_equal 1 }
|
39
|
+
|
40
|
+
proxy.clear_cache
|
41
|
+
|
42
|
+
proxy.next_x.must_equal 2
|
43
|
+
proxy.next_y.must_equal 2
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'Clear cache for specific method' do
|
47
|
+
object = Fake.new
|
48
|
+
proxy = Caching::Proxy.new object, :next_x, :next_y
|
49
|
+
|
50
|
+
3.times{ proxy.next_x.must_equal 1 }
|
51
|
+
3.times{ proxy.next_y.must_equal 1 }
|
52
|
+
|
53
|
+
proxy.clear_cache :next_x
|
54
|
+
|
55
|
+
proxy.next_x.must_equal 2
|
56
|
+
proxy.next_y.must_equal 1
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
describe Caching::Storage do
|
4
|
+
|
5
|
+
it 'Write and Read' do
|
6
|
+
storage = Caching::Storage.new
|
7
|
+
storage.write :key_1, 'value_1'
|
8
|
+
storage.write :key_2, 'value_2'
|
9
|
+
|
10
|
+
storage.read(:key_1).must_equal 'value_1'
|
11
|
+
storage.read(:key_2).must_equal 'value_2'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'Fetch' do
|
15
|
+
storage = Caching::Storage.new
|
16
|
+
number = 0
|
17
|
+
|
18
|
+
storage.fetch(:key) { number += 1 }.must_equal 1
|
19
|
+
storage.fetch(:key) { number += 1 }.must_equal 1
|
20
|
+
number.must_equal 1
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'Clear all' do
|
24
|
+
storage = Caching::Storage.new
|
25
|
+
storage.write :key_1, 'value_1'
|
26
|
+
storage.write :key_2, 'value_2'
|
27
|
+
|
28
|
+
storage.clear
|
29
|
+
|
30
|
+
storage.read(:key_1).must_be_nil
|
31
|
+
storage.read(:key_2).must_be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'Clear specific keys' do
|
35
|
+
storage = Caching::Storage.new
|
36
|
+
storage.write :key_1, 'value_1'
|
37
|
+
storage.write :key_2, 'value_2'
|
38
|
+
storage.write :key_3, 'value_3'
|
39
|
+
|
40
|
+
storage.clear :key_1, :key_3
|
41
|
+
|
42
|
+
storage.read(:key_1).must_be_nil
|
43
|
+
storage.read(:key_2).must_equal 'value_2'
|
44
|
+
storage.read(:key_3).must_be_nil
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: caching
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gabriel Naiman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '4.7'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.7'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: turn
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.9'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.9'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: simplecov
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Cache methods
|
95
|
+
email:
|
96
|
+
- gnaiman@keepcon.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .coveralls.yml
|
102
|
+
- .gitignore
|
103
|
+
- .travis.yml
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- caching.gemspec
|
109
|
+
- lib/caching.rb
|
110
|
+
- lib/caching/proxy.rb
|
111
|
+
- lib/caching/storage.rb
|
112
|
+
- lib/caching/version.rb
|
113
|
+
- spec/caching_spec.rb
|
114
|
+
- spec/coverage_helper.rb
|
115
|
+
- spec/minitest_helper.rb
|
116
|
+
- spec/proxy_spec.rb
|
117
|
+
- spec/storage_spec.rb
|
118
|
+
homepage: https://github.com/gabynaiman/caching
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ! '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.8.24
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: Cache methods
|
143
|
+
test_files:
|
144
|
+
- spec/caching_spec.rb
|
145
|
+
- spec/coverage_helper.rb
|
146
|
+
- spec/minitest_helper.rb
|
147
|
+
- spec/proxy_spec.rb
|
148
|
+
- spec/storage_spec.rb
|
149
|
+
has_rdoc:
|