cache_me 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.
- data/.gitignore +5 -0
- data/Gemfile +6 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/cache_me.gemspec +27 -0
- data/lib/cache_me.rb +24 -0
- data/lib/cache_me/cacheable.rb +78 -0
- data/lib/cache_me/version.rb +3 -0
- metadata +103 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
Overview
|
2
|
+
========
|
3
|
+
|
4
|
+
About CacheMe
|
5
|
+
-------------
|
6
|
+
|
7
|
+
CacheMe is Rails Gem to support Object caching in Rails.
|
8
|
+
|
9
|
+
|
10
|
+
THIS STUFF IS IN ALPHA MODE. NOT SUITABLE FOR PRODUCTION RIGHT NOW.
|
11
|
+
|
12
|
+
License
|
13
|
+
-------
|
14
|
+
|
15
|
+
Copyright (c) 2009, 2010, 2011 Arun Agrawal
|
16
|
+
|
17
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
18
|
+
a copy of this software and associated documentation files (the
|
19
|
+
"Software"), to deal in the Software without restriction, including
|
20
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
21
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
22
|
+
permit persons to whom the Software is furnished to do so, subject to
|
23
|
+
the following conditions:
|
24
|
+
|
25
|
+
The above copyright notice and this permission notice shall be
|
26
|
+
included in all copies or substantial portions of the Software.
|
27
|
+
|
28
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
29
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
30
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
31
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
32
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
33
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
34
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
35
|
+
|
36
|
+
Credits
|
37
|
+
-------
|
38
|
+
|
39
|
+
Arun Agrawal: arunagw at gmail dot com
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/cache_me.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "cache_me/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "cache_me"
|
7
|
+
s.version = CacheMe::VERSION
|
8
|
+
s.authors = ["Arun Agrawal"]
|
9
|
+
s.email = ["arunagw@gmail.com"]
|
10
|
+
s.homepage = "http://cacheme.rubyrockers.com"
|
11
|
+
s.summary = %q{Cache your objects into any cache store}
|
12
|
+
s.description = %q{CacheMe allows you to cache objects of activerecord}
|
13
|
+
|
14
|
+
s.rubyforge_project = "cache_me"
|
15
|
+
|
16
|
+
s.add_dependency("activesupport", ["~> 3.0"])
|
17
|
+
s.add_dependency("activerecord", ["~> 3.0"])
|
18
|
+
|
19
|
+
s.add_development_dependency("rdoc", ["~> 3.5.0"])
|
20
|
+
s.add_development_dependency("sqlite3")
|
21
|
+
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
end
|
data/lib/cache_me.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_record'
|
3
|
+
require "cache_me/version"
|
4
|
+
require "cache_me/cacheable"
|
5
|
+
|
6
|
+
module CacheMe
|
7
|
+
def self.included(base)
|
8
|
+
base.send :extend, ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
include Cacheable
|
13
|
+
|
14
|
+
def cache_me
|
15
|
+
send :include, InstanceMethods
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
include Cacheable
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveRecord::Base.send :include, CacheMe
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module CacheMe
|
2
|
+
module Cacheable
|
3
|
+
|
4
|
+
extend ActiveSupport::Memoizable
|
5
|
+
|
6
|
+
def expire_cache(method_name = nil, args = {})
|
7
|
+
with = args[:with]
|
8
|
+
delete_cache(method_cache_key(method_name, with))
|
9
|
+
end
|
10
|
+
memoize :expire_cache
|
11
|
+
alias :clear_cache :expire_cache
|
12
|
+
|
13
|
+
def cached(method_name, args = {})
|
14
|
+
with = args[:with]
|
15
|
+
|
16
|
+
# TODO : Can use fetch here
|
17
|
+
key = method_cache_key(method_name, with)
|
18
|
+
if cache_exist?(key)
|
19
|
+
read_cache(key)
|
20
|
+
else
|
21
|
+
write_cache(key, do_send(method_name, with))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
memoize :cached
|
25
|
+
alias :caches :cached
|
26
|
+
|
27
|
+
def fetch_cache(method_name, args = {})
|
28
|
+
with = args[:with]
|
29
|
+
read_cache(method_cache_key(method_name, with))
|
30
|
+
end
|
31
|
+
memoize :fetch_cache
|
32
|
+
|
33
|
+
def write_cache(key, value)
|
34
|
+
Rails.cache.write(key, value)
|
35
|
+
value
|
36
|
+
end
|
37
|
+
memoize :write_cache
|
38
|
+
|
39
|
+
def read_cache(key)
|
40
|
+
Rails.cache.read(key)
|
41
|
+
end
|
42
|
+
memoize :read_cache
|
43
|
+
|
44
|
+
def delete_cache(key)
|
45
|
+
Rails.cache.delete(key)
|
46
|
+
true
|
47
|
+
end
|
48
|
+
memoize :delete_cache
|
49
|
+
|
50
|
+
def cache_exist?(key)
|
51
|
+
Rails.cache.exist?(key)
|
52
|
+
end
|
53
|
+
memoize :cache_exist?
|
54
|
+
|
55
|
+
def method_cache_key(method_name, with)
|
56
|
+
if self.is_a?(ActiveRecord::Base)
|
57
|
+
base = [self.class, id, method_name].compact.join(':').gsub(' ', '_')
|
58
|
+
else
|
59
|
+
base = "#{self}:#{method_name}"
|
60
|
+
end
|
61
|
+
if with.blank?
|
62
|
+
base
|
63
|
+
else
|
64
|
+
"#{base}:#{with}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
memoize :method_cache_key
|
68
|
+
|
69
|
+
def do_send(method_name, with)
|
70
|
+
if with.blank?
|
71
|
+
send(method_name)
|
72
|
+
else
|
73
|
+
send(method_name, with)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_me
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Arun Agrawal
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-28 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70192474481780 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70192474481780
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &70192474478160 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70192474478160
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rdoc
|
38
|
+
requirement: &70192474475500 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.5.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70192474475500
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &70192474473220 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70192474473220
|
58
|
+
description: CacheMe allows you to cache objects of activerecord
|
59
|
+
email:
|
60
|
+
- arunagw@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- cache_me.gemspec
|
70
|
+
- lib/cache_me.rb
|
71
|
+
- lib/cache_me/cacheable.rb
|
72
|
+
- lib/cache_me/version.rb
|
73
|
+
homepage: http://cacheme.rubyrockers.com
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: -899571992026705786
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
hash: -899571992026705786
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project: cache_me
|
99
|
+
rubygems_version: 1.8.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Cache your objects into any cache store
|
103
|
+
test_files: []
|