redvex-ar_cache 0.1.0
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/CHANGELOG +1 -0
- data/Manifest +7 -0
- data/README.rdoc +59 -0
- data/Rakefile +14 -0
- data/ar_cache.gemspec +34 -0
- data/init.rb +1 -0
- data/lib/ar_cache.rb +121 -0
- metadata +76 -0
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0 - Initial release
|
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
Performe ActiveRecord cache in File System for heavy and repetitive query.
|
2
|
+
|
3
|
+
==Licence
|
4
|
+
|
5
|
+
Copyright 2009 Redvex. Distributes under the same terms as Ruby
|
6
|
+
|
7
|
+
If you use this software, please make a donation[https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7415509]
|
8
|
+
|
9
|
+
==Features
|
10
|
+
|
11
|
+
* Perform any ActiveRecord Query
|
12
|
+
* Set time of expire in database configuration
|
13
|
+
* Obtain an Hash as u obtain from active record
|
14
|
+
* Support for ActiveRecord shortcuts through (cache_find_first, cache_find_last, cache_find_all)
|
15
|
+
|
16
|
+
=Usage
|
17
|
+
|
18
|
+
==Installation
|
19
|
+
* gem sources -a http://gems.github.com
|
20
|
+
* gem install ar_cache
|
21
|
+
|
22
|
+
==Use ActiveRecord Cache
|
23
|
+
|
24
|
+
If you want to perform a standard call using cache u can try
|
25
|
+
|
26
|
+
* Object.cache_find(id, params)
|
27
|
+
|
28
|
+
EX.
|
29
|
+
* Object.cache_find(10)
|
30
|
+
or
|
31
|
+
* Object.cache_find(:all, :conditions => ['field>?',5])
|
32
|
+
|
33
|
+
If you want to use an active record shortcuts you can:
|
34
|
+
|
35
|
+
* Object.cache_find_first(params)
|
36
|
+
* Object.cache_find_last(params)
|
37
|
+
* Object.cache_find_all(params)
|
38
|
+
|
39
|
+
EX.
|
40
|
+
|
41
|
+
* Object.cache_find_first
|
42
|
+
* Object.cache_find_last
|
43
|
+
* Object.cache_find_all(:conditions => ['filed>?', 5])
|
44
|
+
|
45
|
+
==Cache reset
|
46
|
+
If you want to reset cache after predetermined amount of time you can specify time in database.yml config file through cache_expire directive:
|
47
|
+
|
48
|
+
* cache_expire: 30.seconds
|
49
|
+
* cache_expire: 2.hours
|
50
|
+
* cache_expire: 1.day
|
51
|
+
* cache_expire: 1.month
|
52
|
+
|
53
|
+
If you want to force the expire the cache manually you can call the method
|
54
|
+
|
55
|
+
* cache_reset!(id, params=nil)
|
56
|
+
or shortcuts
|
57
|
+
* cache_reset_first!(params=nil)
|
58
|
+
* cache_reset_last!(params=nil)
|
59
|
+
* cache_reset_all!(params=nil)
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('ar_cache', '0.1.0') do |p|
|
6
|
+
p.description = "Performe ActiveRecord cache in File System for heavy and repetitive query."
|
7
|
+
p.url = "http://github.com/redvex/ar_cache"
|
8
|
+
p.author = "Gianni Mazza"
|
9
|
+
p.email = "redvex@me.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.runtime_dependencies = ['json']
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/ar_cache.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{ar_cache}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Gianni Mazza"]
|
9
|
+
s.date = %q{2009-08-11}
|
10
|
+
s.description = %q{Performe ActiveRecord cache in File System for heavy and repetitive query.}
|
11
|
+
s.email = %q{redvex@me.com}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/ar_cache.rb", "README.rdoc"]
|
13
|
+
s.files = ["ar_cache.gemspec", "CHANGELOG", "init.rb", "lib/ar_cache.rb", "Manifest", "Rakefile", "README.rdoc"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/redvex/ar_cache}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ar_cache", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{ar_cache}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Performe ActiveRecord cache in File System for heavy and repetitive query.}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<json>, [">= 0"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<json>, [">= 0"])
|
33
|
+
end
|
34
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ar_cache'
|
data/lib/ar_cache.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# AR_Cache o ActiveRecord Cache Performe ActiveRecord
|
2
|
+
# cache in File System for heavy and repetitive query.
|
3
|
+
#
|
4
|
+
# Author:: Gianni Mazza (mailto:redvex@me.com)
|
5
|
+
# Copyright:: Copyright (c) Redvex
|
6
|
+
# License:: Distributes under the same terms as Ruby
|
7
|
+
|
8
|
+
module ArCache
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
def ensure_unique(name)
|
14
|
+
begin
|
15
|
+
self[name] = yield
|
16
|
+
end while self.class.exists?(name => self[name])
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
# Perform a query on dabatase and store result in FileSystem.
|
21
|
+
#
|
22
|
+
# To uniquify the file name the library calculate MD5 hash for classname, id and params,
|
23
|
+
# if result for query exist and isn't expired it return the cached result otherwise DB
|
24
|
+
# is queried and the results is stored in FileSystem.
|
25
|
+
# TODO: Support for Eeager-loading
|
26
|
+
def cache_find(id, params=nil)
|
27
|
+
class_name = self.class.name
|
28
|
+
|
29
|
+
if (params.nil?)
|
30
|
+
params = Array.new
|
31
|
+
end
|
32
|
+
|
33
|
+
filename = calculate_file_name(id, params)
|
34
|
+
if cache_reset?(filename)
|
35
|
+
cache_reset!(id, params)
|
36
|
+
end
|
37
|
+
|
38
|
+
if File.exist?(filename)
|
39
|
+
content = File.read(filename)
|
40
|
+
toReturn = JSON.parse(content)
|
41
|
+
else
|
42
|
+
toReturn = self.find(id, params)
|
43
|
+
File.open(filename, 'w') do |f|
|
44
|
+
f.write toReturn.to_json
|
45
|
+
f.close
|
46
|
+
end
|
47
|
+
toReturn = JSON.parse(toReturn.to_json)
|
48
|
+
end
|
49
|
+
if toReturn.count > 1
|
50
|
+
out = Array.new
|
51
|
+
toReturn.each do |obj|
|
52
|
+
out << hash_to_obj(obj.values.first)
|
53
|
+
end
|
54
|
+
else
|
55
|
+
toReturn = toReturn.first
|
56
|
+
out = hash_to_obj(toReturn.values.first)
|
57
|
+
end
|
58
|
+
return out
|
59
|
+
end
|
60
|
+
|
61
|
+
def cache_find_first(params=nil)
|
62
|
+
return cache_find(:first, params)
|
63
|
+
end
|
64
|
+
|
65
|
+
def cache_find_last(params=nil)
|
66
|
+
return cache_find(:last, params)
|
67
|
+
end
|
68
|
+
|
69
|
+
def cache_find_all(params=nil)
|
70
|
+
return cache_find(:all, params)
|
71
|
+
end
|
72
|
+
|
73
|
+
def cache_reset_first!(params=nil)
|
74
|
+
return cache_reset(:first, params)
|
75
|
+
end
|
76
|
+
|
77
|
+
def cache_reset_last!(params=nil)
|
78
|
+
return cache_reset(:last, params)
|
79
|
+
end
|
80
|
+
|
81
|
+
def cache_reset_all!(params=nil)
|
82
|
+
return cache_reset(:all, params)
|
83
|
+
end
|
84
|
+
|
85
|
+
def cache_reset!(id, params=nil)
|
86
|
+
filename = calculate_file_name(id, params)
|
87
|
+
File.delete(filename)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
def hash_to_obj(hash)
|
92
|
+
tmp = self.new
|
93
|
+
hash.each do |k,v|
|
94
|
+
unless v.nil? or v.is_a?(Numeric)
|
95
|
+
v = v.gsub('"',"'")
|
96
|
+
end
|
97
|
+
eval(sprintf('tmp.%s = "%s"',k,v))
|
98
|
+
end
|
99
|
+
return tmp
|
100
|
+
end
|
101
|
+
|
102
|
+
def cache_reset?(file)
|
103
|
+
filename = File.join(RAILS_ROOT, "config", "database.yml")
|
104
|
+
dbconf = YAML.load_file(filename)[RAILS_ENV]
|
105
|
+
if dbconf["cache_expire"].nil?
|
106
|
+
return false
|
107
|
+
else
|
108
|
+
return File.open(file).ctime + dbconf["cache_expire"] > Time.now
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def calculate_file_name(id, params)
|
113
|
+
File.join(RAILS_ROOT, "tmp", "cache",
|
114
|
+
Digest::MD5.hexdigest(class_name + "--" + id.to_s + "--"+ params.values.join("--"))+".query")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class ActiveRecord::Base
|
120
|
+
include ArCache
|
121
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redvex-ar_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gianni Mazza
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-11 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Performe ActiveRecord cache in File System for heavy and repetitive query.
|
26
|
+
email: redvex@me.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- CHANGELOG
|
33
|
+
- lib/ar_cache.rb
|
34
|
+
- README.rdoc
|
35
|
+
files:
|
36
|
+
- ar_cache.gemspec
|
37
|
+
- CHANGELOG
|
38
|
+
- init.rb
|
39
|
+
- lib/ar_cache.rb
|
40
|
+
- Manifest
|
41
|
+
- Rakefile
|
42
|
+
- README.rdoc
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/redvex/ar_cache
|
45
|
+
licenses:
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --line-numbers
|
49
|
+
- --inline-source
|
50
|
+
- --title
|
51
|
+
- Ar_cache
|
52
|
+
- --main
|
53
|
+
- README.rdoc
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "1.2"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: ar_cache
|
71
|
+
rubygems_version: 1.3.5
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: Performe ActiveRecord cache in File System for heavy and repetitive query.
|
75
|
+
test_files: []
|
76
|
+
|