cache_version 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README.rdoc +37 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/examples/sample_migration.rb +9 -0
- data/lib/cache_version.rb +82 -0
- data/test/cache_version_test.rb +25 -0
- data/test/test_helper.rb +23 -0
- metadata +75 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Justin Balthrop
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
= CacheVersion
|
2
|
+
|
3
|
+
CacheVersion lets you maintain a version for any class. This can be used for cache
|
4
|
+
invalidation, and RecordCache and MethodCache use it for that. It uses memcache to reduce
|
5
|
+
database access when the version of a class hasn't changed.
|
6
|
+
|
7
|
+
== Usage:
|
8
|
+
|
9
|
+
CacheVersion.get(User)
|
10
|
+
# => 0
|
11
|
+
|
12
|
+
CacheVersion.increment(User)
|
13
|
+
CacheVersion.get(User)
|
14
|
+
# => 1
|
15
|
+
|
16
|
+
# Or you can use the alternate syntax:
|
17
|
+
|
18
|
+
User.version
|
19
|
+
# => 1
|
20
|
+
|
21
|
+
User.increment_version
|
22
|
+
User.version
|
23
|
+
# => 2
|
24
|
+
|
25
|
+
== Install:
|
26
|
+
|
27
|
+
sudo gem install cache-version -s http://gemcutter.org
|
28
|
+
|
29
|
+
Also, you need to create a migration to make the cache_versions table. See examples/sample_migration.rb
|
30
|
+
|
31
|
+
== Dependencies:
|
32
|
+
|
33
|
+
* {memcache}[http://github.com/ninjudd/memcache]
|
34
|
+
|
35
|
+
== License:
|
36
|
+
|
37
|
+
Copyright (c) 2009 Justin Balthrop, Geni.com; Published under The MIT License, see LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |s|
|
8
|
+
s.name = "cache_version"
|
9
|
+
s.summary = %Q{Store the version of any class for cache invalidation}
|
10
|
+
s.email = "code@justinbalthrop.com"
|
11
|
+
s.homepage = "http://github.com/ninjudd/cache_version"
|
12
|
+
s.description = "Store the version of any class for cache invalidation"
|
13
|
+
s.authors = ["Justin Balthrop"]
|
14
|
+
s.add_dependency('memcache', '>= 1.0.0')
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
Rake::TestTask.new do |t|
|
22
|
+
t.libs << 'lib'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::RDocTask.new do |rdoc|
|
28
|
+
rdoc.rdoc_dir = 'rdoc'
|
29
|
+
rdoc.title = 'cache_version'
|
30
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
31
|
+
rdoc.rdoc_files.include('README*')
|
32
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |t|
|
38
|
+
t.libs << 'test'
|
39
|
+
t.test_files = FileList['test/**/*_test.rb']
|
40
|
+
t.verbose = true
|
41
|
+
end
|
42
|
+
rescue LoadError
|
43
|
+
end
|
44
|
+
|
45
|
+
task :default => :test
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.9.4
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'memcache'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module CacheVersion
|
6
|
+
def self.db
|
7
|
+
db = ActiveRecord::Base.connection
|
8
|
+
if defined?(DataFabric::ConnectionProxy) and db.kind_of?(DataFabric::ConnectionProxy)
|
9
|
+
db.send(:master)
|
10
|
+
else
|
11
|
+
db
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.cache
|
16
|
+
CACHE
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.get(key)
|
20
|
+
key = key.to_s
|
21
|
+
version_by_key[key] ||= CACHE.get_or_add(cache_key(key)) do
|
22
|
+
db.select_value("SELECT version FROM cache_versions WHERE key = '#{key}'").to_i
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.increment(key)
|
27
|
+
key = key.to_s
|
28
|
+
if get(key) == 0
|
29
|
+
db.execute("INSERT INTO cache_versions (key, version) VALUES ('#{key}', 1)")
|
30
|
+
else
|
31
|
+
db.execute("UPDATE cache_versions SET version = version + 1 WHERE key = '#{key}'")
|
32
|
+
end
|
33
|
+
invalidate_cache(key)
|
34
|
+
get(key)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.invalidate_cache(key)
|
38
|
+
key = key.to_s
|
39
|
+
cache.delete(cache_key(key))
|
40
|
+
version_by_key.delete(key)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.clear_cache
|
44
|
+
@version_by_key = {}
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def self.version_by_key
|
49
|
+
@version_by_key ||= {}
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.cache_key(key)
|
53
|
+
"v:#{key}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Module
|
58
|
+
def version(context = nil)
|
59
|
+
key = [self, context].compact.join('_')
|
60
|
+
CacheVersion.get(key)
|
61
|
+
end
|
62
|
+
|
63
|
+
def increment_version(context = nil)
|
64
|
+
key = [self, context].compact.join('_')
|
65
|
+
CacheVersion.increment(key)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class CacheVersionMigration < ActiveRecord::Migration
|
70
|
+
def self.up
|
71
|
+
create_table :cache_versions, :id => false do |t|
|
72
|
+
t.column :key, :string
|
73
|
+
t.column :version, :integer, :default => 0
|
74
|
+
end
|
75
|
+
|
76
|
+
add_index :cache_versions, :key, :unique => true
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.down
|
80
|
+
drop_table :cache_versions
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class CacheVersionTest < Test::Unit::TestCase
|
4
|
+
context 'with a memcache and db connection' do
|
5
|
+
setup do
|
6
|
+
system('memcached -d')
|
7
|
+
CacheVersionMigration.up
|
8
|
+
end
|
9
|
+
|
10
|
+
teardown do
|
11
|
+
system('killall memcached')
|
12
|
+
CacheVersionMigration.down
|
13
|
+
end
|
14
|
+
|
15
|
+
should 'increment cache version' do
|
16
|
+
5.times do |i|
|
17
|
+
assert_equal i, Object.version
|
18
|
+
Object.increment_version
|
19
|
+
assert_equal i + 1, Object.version
|
20
|
+
end
|
21
|
+
CacheVersion.clear_cache
|
22
|
+
assert_equal 5, Object.version
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
7
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../../memcache/lib"
|
8
|
+
|
9
|
+
require 'cache_version'
|
10
|
+
|
11
|
+
class Test::Unit::TestCase
|
12
|
+
end
|
13
|
+
|
14
|
+
CACHE = Memcache.new(:servers => 'localhost')
|
15
|
+
ActiveRecord::Base.establish_connection(
|
16
|
+
:adapter => "postgresql",
|
17
|
+
:host => "localhost",
|
18
|
+
:username => "postgres",
|
19
|
+
:password => "",
|
20
|
+
:database => "test"
|
21
|
+
)
|
22
|
+
ActiveRecord::Migration.verbose = false
|
23
|
+
ActiveRecord::Base.connection.client_min_messages = 'panic'
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Balthrop
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-20 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: memcache
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
version:
|
25
|
+
description: Store the version of any class for cache invalidation
|
26
|
+
email: code@justinbalthrop.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- LICENSE
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
|
+
- VERSION
|
40
|
+
- examples/sample_migration.rb
|
41
|
+
- lib/cache_version.rb
|
42
|
+
- test/cache_version_test.rb
|
43
|
+
- test/test_helper.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/ninjudd/cache_version
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Store the version of any class for cache invalidation
|
72
|
+
test_files:
|
73
|
+
- test/cache_version_test.rb
|
74
|
+
- test/test_helper.rb
|
75
|
+
- examples/sample_migration.rb
|