cache-machine-redis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ # Packed ge
2
+ *.gem
3
+
4
+ # RubyMine metadata
5
+ .idea
6
+
7
+ # rcov generated
8
+ coverage
9
+
10
+ # rdoc generated
11
+ rdoc
12
+
13
+ # yard generated
14
+ doc
15
+ .yardoc
16
+
17
+ # bundler
18
+ .bundle
19
+
20
+ # jeweler generated
21
+ pkg
22
+
23
+ .rspec
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ script: bundle exec rspec spec/lib/cache_machine/adapters/redis_spec.rb
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails"
4
+ gem "cache-machine", "~> 0.2.0"
5
+ gem 'redis'
6
+
7
+ group :development do
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.6.2"
10
+ end
11
+
12
+ group :test do
13
+ gem "rcov", ">= 0"
14
+ gem 'rspec'
15
+ gem 'rspec-rails'
16
+ gem "sqlite3-ruby"
17
+ end
@@ -0,0 +1,107 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.9)
6
+ actionpack (= 3.0.9)
7
+ mail (~> 2.2.19)
8
+ actionpack (3.0.9)
9
+ activemodel (= 3.0.9)
10
+ activesupport (= 3.0.9)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.9)
19
+ activesupport (= 3.0.9)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.9)
23
+ activemodel (= 3.0.9)
24
+ activesupport (= 3.0.9)
25
+ arel (~> 2.0.10)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.9)
28
+ activemodel (= 3.0.9)
29
+ activesupport (= 3.0.9)
30
+ activesupport (3.0.9)
31
+ arel (2.0.10)
32
+ builder (2.1.2)
33
+ cache-machine (0.2.0)
34
+ rails (>= 3)
35
+ diff-lcs (1.1.2)
36
+ erubis (2.6.6)
37
+ abstract (>= 1.0.0)
38
+ git (1.2.5)
39
+ i18n (0.5.0)
40
+ jeweler (1.6.2)
41
+ bundler (~> 1.0)
42
+ git (>= 1.2.5)
43
+ rake
44
+ mail (2.2.19)
45
+ activesupport (>= 2.3.6)
46
+ i18n (>= 0.4.0)
47
+ mime-types (~> 1.16)
48
+ treetop (~> 1.4.8)
49
+ mime-types (1.16)
50
+ polyglot (0.3.1)
51
+ rack (1.2.3)
52
+ rack-mount (0.6.14)
53
+ rack (>= 1.0.0)
54
+ rack-test (0.5.7)
55
+ rack (>= 1.0)
56
+ rails (3.0.9)
57
+ actionmailer (= 3.0.9)
58
+ actionpack (= 3.0.9)
59
+ activerecord (= 3.0.9)
60
+ activeresource (= 3.0.9)
61
+ activesupport (= 3.0.9)
62
+ bundler (~> 1.0)
63
+ railties (= 3.0.9)
64
+ railties (3.0.9)
65
+ actionpack (= 3.0.9)
66
+ activesupport (= 3.0.9)
67
+ rake (>= 0.8.7)
68
+ rdoc (~> 3.4)
69
+ thor (~> 0.14.4)
70
+ rake (0.9.2)
71
+ rcov (0.9.9)
72
+ rdoc (3.6.1)
73
+ redis (2.2.2)
74
+ rspec (2.6.0)
75
+ rspec-core (~> 2.6.0)
76
+ rspec-expectations (~> 2.6.0)
77
+ rspec-mocks (~> 2.6.0)
78
+ rspec-core (2.6.4)
79
+ rspec-expectations (2.6.0)
80
+ diff-lcs (~> 1.1.2)
81
+ rspec-mocks (2.6.0)
82
+ rspec-rails (2.6.1)
83
+ actionpack (~> 3.0)
84
+ activesupport (~> 3.0)
85
+ railties (~> 3.0)
86
+ rspec (~> 2.6.0)
87
+ sqlite3 (1.3.3)
88
+ sqlite3-ruby (1.3.3)
89
+ sqlite3 (>= 1.3.3)
90
+ thor (0.14.6)
91
+ treetop (1.4.9)
92
+ polyglot (>= 0.3.1)
93
+ tzinfo (0.3.28)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ bundler (~> 1.0.0)
100
+ cache-machine (~> 0.2.0)
101
+ jeweler (~> 1.6.2)
102
+ rails
103
+ rcov
104
+ redis
105
+ rspec
106
+ rspec-rails
107
+ sqlite3-ruby
@@ -0,0 +1,175 @@
1
+ Copyright (c) 2011 PartyEarth LLC
2
+
3
+ GNU LESSER GENERAL PUBLIC LICENSE
4
+ Version 3, 29 June 2007
5
+
6
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
7
+ Everyone is permitted to copy and distribute verbatim copies
8
+ of this license document, but changing it is not allowed.
9
+
10
+
11
+ This version of the GNU Lesser General Public License incorporates
12
+ the terms and conditions of version 3 of the GNU General Public
13
+ License, supplemented by the additional permissions listed below.
14
+
15
+ 0. Additional Definitions.
16
+
17
+ As used herein, "this License" refers to version 3 of the GNU Lesser
18
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
19
+ General Public License.
20
+
21
+ "The Library" refers to a covered work governed by this License,
22
+ other than an Application or a Combined Work as defined below.
23
+
24
+ An "Application" is any work that makes use of an interface provided
25
+ by the Library, but which is not otherwise based on the Library.
26
+ Defining a subclass of a class defined by the Library is deemed a mode
27
+ of using an interface provided by the Library.
28
+
29
+ A "Combined Work" is a work produced by combining or linking an
30
+ Application with the Library. The particular version of the Library
31
+ with which the Combined Work was made is also called the "Linked
32
+ Version".
33
+
34
+ The "Minimal Corresponding Source" for a Combined Work means the
35
+ Corresponding Source for the Combined Work, excluding any source code
36
+ for portions of the Combined Work that, considered in isolation, are
37
+ based on the Application, and not on the Linked Version.
38
+
39
+ The "Corresponding Application Code" for a Combined Work means the
40
+ object code and/or source code for the Application, including any data
41
+ and utility programs needed for reproducing the Combined Work from the
42
+ Application, but excluding the System Libraries of the Combined Work.
43
+
44
+ 1. Exception to Section 3 of the GNU GPL.
45
+
46
+ You may convey a covered work under sections 3 and 4 of this License
47
+ without being bound by section 3 of the GNU GPL.
48
+
49
+ 2. Conveying Modified Versions.
50
+
51
+ If you modify a copy of the Library, and, in your modifications, a
52
+ facility refers to a function or data to be supplied by an Application
53
+ that uses the facility (other than as an argument passed when the
54
+ facility is invoked), then you may convey a copy of the modified
55
+ version:
56
+
57
+ a) under this License, provided that you make a good faith effort to
58
+ ensure that, in the event an Application does not supply the
59
+ function or data, the facility still operates, and performs
60
+ whatever part of its purpose remains meaningful, or
61
+
62
+ b) under the GNU GPL, with none of the additional permissions of
63
+ this License applicable to that copy.
64
+
65
+ 3. Object Code Incorporating Material from Library Header Files.
66
+
67
+ The object code form of an Application may incorporate material from
68
+ a header file that is part of the Library. You may convey such object
69
+ code under terms of your choice, provided that, if the incorporated
70
+ material is not limited to numerical parameters, data structure
71
+ layouts and accessors, or small macros, inline functions and templates
72
+ (ten or fewer lines in length), you do both of the following:
73
+
74
+ a) Give prominent notice with each copy of the object code that the
75
+ Library is used in it and that the Library and its use are
76
+ covered by this License.
77
+
78
+ b) Accompany the object code with a copy of the GNU GPL and this license
79
+ document.
80
+
81
+ 4. Combined Works.
82
+
83
+ You may convey a Combined Work under terms of your choice that,
84
+ taken together, effectively do not restrict modification of the
85
+ portions of the Library contained in the Combined Work and reverse
86
+ engineering for debugging such modifications, if you also do each of
87
+ the following:
88
+
89
+ a) Give prominent notice with each copy of the Combined Work that
90
+ the Library is used in it and that the Library and its use are
91
+ covered by this License.
92
+
93
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
94
+ document.
95
+
96
+ c) For a Combined Work that displays copyright notices during
97
+ execution, include the copyright notice for the Library among
98
+ these notices, as well as a reference directing the user to the
99
+ copies of the GNU GPL and this license document.
100
+
101
+ d) Do one of the following:
102
+
103
+ 0) Convey the Minimal Corresponding Source under the terms of this
104
+ License, and the Corresponding Application Code in a form
105
+ suitable for, and under terms that permit, the user to
106
+ recombine or relink the Application with a modified version of
107
+ the Linked Version to produce a modified Combined Work, in the
108
+ manner specified by section 6 of the GNU GPL for conveying
109
+ Corresponding Source.
110
+
111
+ 1) Use a suitable shared library mechanism for linking with the
112
+ Library. A suitable mechanism is one that (a) uses at run time
113
+ a copy of the Library already present on the user's computer
114
+ system, and (b) will operate properly with a modified version
115
+ of the Library that is interface-compatible with the Linked
116
+ Version.
117
+
118
+ e) Provide Installation Information, but only if you would otherwise
119
+ be required to provide such information under section 6 of the
120
+ GNU GPL, and only to the extent that such information is
121
+ necessary to install and execute a modified version of the
122
+ Combined Work produced by recombining or relinking the
123
+ Application with a modified version of the Linked Version. (If
124
+ you use option 4d0, the Installation Information must accompany
125
+ the Minimal Corresponding Source and Corresponding Application
126
+ Code. If you use option 4d1, you must provide the Installation
127
+ Information in the manner specified by section 6 of the GNU GPL
128
+ for conveying Corresponding Source.)
129
+
130
+ 5. Combined Libraries.
131
+
132
+ You may place library facilities that are a work based on the
133
+ Library side by side in a single library together with other library
134
+ facilities that are not Applications and are not covered by this
135
+ License, and convey such a combined library under terms of your
136
+ choice, if you do both of the following:
137
+
138
+ a) Accompany the combined library with a copy of the same work based
139
+ on the Library, uncombined with any other library facilities,
140
+ conveyed under the terms of this License.
141
+
142
+ b) Give prominent notice with the combined library that part of it
143
+ is a work based on the Library, and explaining where to find the
144
+ accompanying uncombined form of the same work.
145
+
146
+ 6. Revised Versions of the GNU Lesser General Public License.
147
+
148
+ The Free Software Foundation may publish revised and/or new versions
149
+ of the GNU Lesser General Public License from time to time. Such new
150
+ versions will be similar in spirit to the present version, but may
151
+ differ in detail to address new problems or concerns.
152
+
153
+ Each version is given a distinguishing version number. If the
154
+ Library as you received it specifies that a certain numbered version
155
+ of the GNU Lesser General Public License "or any later version"
156
+ applies to it, you have the option of following the terms and
157
+ conditions either of that published version or of any later version
158
+ published by the Free Software Foundation. If the Library as you
159
+ received it does not specify a version number of the GNU Lesser
160
+ General Public License, you may choose any version of the GNU Lesser
161
+ General Public License ever published by the Free Software Foundation.
162
+
163
+ If the Library as you received it specifies that a proxy can decide
164
+ whether future versions of the GNU Lesser General Public License shall
165
+ apply, that proxy's public statement of acceptance of any version is
166
+ permanent authorization for you to choose that version for the
167
+ Library.
168
+
169
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
170
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
171
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
172
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
173
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
174
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
175
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # ![cache-machine](http://img195.imageshack.us/img195/5371/cachemachinefinal2.png)
2
+
3
+ Redis adapter for Cache Machine: http://github.com/partyearth/cache-machine
4
+
5
+ ## Installing
6
+
7
+ In your config/environments/env.rb file:
8
+
9
+ ```ruby
10
+ url = "redis://user:pass@host.com:9383/"
11
+ uri = URI.parse(url)
12
+ CacheMachine::Cache.timestamps_adapter = CacheMachine::Adapters::Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
13
+ ```
14
+
15
+ Also note, you can set _storage_adapter_ and _map_adapter_ as well.
16
+
17
+ ## Contributing to cache-machine-redis
18
+
19
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
20
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
21
+ * Fork the project
22
+ * Start a feature/bugfix branch
23
+ * Commit and push until you are happy with your contribution
24
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
25
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
26
+
27
+ ## Copyright
28
+
29
+ Copyright (c) 2011 PartyEarth LLC. See LICENSE.txt for details.
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ ENV['BUNDLE_GEMFILE'] = 'Gemfile'
4
+
5
+ gem_root = File.expand_path(File.dirname(__FILE__))
6
+
7
+ require 'rubygems'
8
+ require 'bundler'
9
+ require 'rake'
10
+ require 'rake'
11
+ require 'rake/testtask'
12
+ require 'rspec'
13
+ require 'rspec/core/rake_task'
14
+
15
+ begin
16
+ Bundler.setup(:default, :development)
17
+ rescue Bundler::BundlerError => e
18
+ $stderr.puts e.message
19
+ $stderr.puts "Run `bundle install` to install missing gems"
20
+ exit e.status_code
21
+ end
22
+
23
+ task :default => :spec
24
+
25
+ desc "Run the test suite"
26
+ task :spec => ['spec:setup', 'spec:lib', 'spec:cleanup']
27
+
28
+ namespace :spec do
29
+ desc "Setup the test environment"
30
+ task :setup do
31
+ system "cd #{gem_root} && bundle install && mkdir db"
32
+ end
33
+
34
+ desc "Cleanup the test environment"
35
+ task :cleanup do
36
+ FileUtils.rm_rf "#{gem_root}/db"
37
+ end
38
+
39
+ desc "Test the Cache Machine lib"
40
+ RSpec::Core::RakeTask.new(:lib) do |task|
41
+ task.pattern = gem_root + '/spec/lib/**/*_spec.rb'
42
+ end
43
+ end
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "cache-machine-redis #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{cache-machine-redis}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Sergei Zinin"]
9
+ s.date = %q{2012-03-05}
10
+ s.description = %q{Redis adapter for Cache Machine gem.}
11
+ s.email = %q{szinin@partyearth.com}
12
+ s.extra_rdoc_files = %w{LICENSE.txt README.md}
13
+ s.files = `git ls-files`.split("\n")
14
+ s.homepage = %q{http://github.com/zininserge/cache-machine-redis}
15
+ s.licenses = ["MIT"]
16
+ s.require_paths = ["lib"]
17
+ s.rubygems_version = %q{1.6.2}
18
+ s.summary = %q{Allows Cache Machine to deal with Redis.}
19
+
20
+ if s.respond_to? :specification_version then
21
+ s.specification_version = 3
22
+
23
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
24
+ s.add_runtime_dependency(%q<cache-machine>, [">= 0.2.0"])
25
+ s.add_runtime_dependency(%q<redis>, [">= 2.2.2"])
26
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
27
+ s.add_development_dependency(%q<rails>, [">= 3"])
28
+ else
29
+ s.add_dependency(%q<cache-machine>, [">= 0.2.0"])
30
+ s.add_dependency(%q<redis>, [">= 2.2.2"])
31
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
32
+ s.add_dependency(%q<rails>, [">= 3"])
33
+ end
34
+ else
35
+ s.add_dependency(%q<cache-machine>, [">= 0.2.0"])
36
+ s.add_dependency(%q<redis>, [">= 2.2.2"])
37
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
38
+ s.add_dependency(%q<rails>, [">= 3"])
39
+ end
40
+ end
@@ -0,0 +1 @@
1
+ *.db
@@ -0,0 +1,6 @@
1
+ #--
2
+ # Copyright (c) 2011 {PartyEarth LLC}[http://partyearth.com]
3
+ # mailto:szinin@partyearth.com
4
+ #++
5
+ require "cache-machine"
6
+ require "cache_machine/adapters/redis"
@@ -0,0 +1,79 @@
1
+ module CacheMachine
2
+ module Adapters
3
+ require "redis"
4
+
5
+ class Redis < CacheMachine::Adapter
6
+ attr_accessor :redis
7
+
8
+ def initialize(*options)
9
+ @redis = ::Redis.new(*options)
10
+ CacheMachine::Logger.info "CACHE_MACHINE: initialized Redis adapter"
11
+ end
12
+
13
+ def append_id_to_map(target, association, id)
14
+ @redis.sadd(get_map_key(target, association), id)
15
+ end
16
+
17
+ def append_id_to_reverse_map(resource, association, target, id)
18
+ @redis.sadd(get_reverse_map_key(resource, association, target), id)
19
+ end
20
+
21
+ def association_ids(target, association)
22
+ get_ids(get_map_key(target, association)) { target.association_ids(association) }
23
+ end
24
+
25
+ def delete(key)
26
+ @redis.del(key).to_i > 0
27
+ end
28
+
29
+ def delete_content(key)
30
+ delete(get_content_key(key))
31
+ end
32
+
33
+ def fetch(key, options = {}, &block)
34
+ key = get_content_key(key)
35
+ block_given? ? exec_multi_command(:setnx, key, options, &block) : @redis.get(key)
36
+ end
37
+
38
+ def fetch_timestamp(name, options = {}, &block)
39
+ exec_multi_command(:setnx, get_timestamp_key(name), options, &block)
40
+ end
41
+
42
+ def reset_timestamp(name)
43
+ @redis.del(get_timestamp_key(name))
44
+ end
45
+
46
+ def reverse_association_ids(resource, association, target)
47
+ get_ids(get_reverse_map_key(resource, association, target)) { target.cache_map_ids(resource, association) }
48
+ end
49
+
50
+ def write_timestamp(name, options = {}, &block)
51
+ exec_multi_command(:set, get_timestamp_key(name), options, &block)
52
+ end
53
+
54
+ protected
55
+
56
+ def exec_multi_command(command, key, options)
57
+ content = yield
58
+
59
+ @redis.multi do
60
+ @redis.send(command, key, content)
61
+ @redis.expire(key, options[:expires_in].to_i) if options[:expires_in]
62
+ end
63
+
64
+ content
65
+ end
66
+
67
+ def get_ids(key)
68
+ if @redis.exists(key)
69
+ @redis.smembers(key)
70
+ else
71
+ # TODO(!#): INVESTIGATE WHY ID CANNOT BE PASSED AS AN ARRAY
72
+ result = yield
73
+ @redis.multi { result.each { |id| @redis.sadd key, id } }
74
+ result
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,83 @@
1
+ TARGET_TABLE_NAME = "cachers"
2
+ HABTM_TABLE_NAME = "has_and_belongs_to_many_cacheables"
3
+ HABTM_JOINS_TABLE_NAME = [TARGET_TABLE_NAME, HABTM_TABLE_NAME].join('_')
4
+ HABTM_ASSOCIATION_NAME = HABTM_TABLE_NAME.singularize
5
+ HM_TABLE_NAME = "has_many_cacheables"
6
+ HMT_JOINS_TABLE_NAME = "joins"
7
+ HMT_TABLE_NAME = "has_many_through_cacheables"
8
+ HO_TABLE_NAME = "hs_one_cacheables"
9
+ POLY_TABLE_NAME = "polymorphics"
10
+ TARGET_ASSOCIATION_NAME = TARGET_TABLE_NAME.singularize
11
+ HMT_ASSOCIATION_NAME = HMT_TABLE_NAME.singularize
12
+
13
+ class TestMigration < ActiveRecord::Migration
14
+ def self.up
15
+ self.down
16
+ create_table(HABTM_TABLE_NAME)
17
+ create_table(HM_TABLE_NAME) { |t| t.references TARGET_ASSOCIATION_NAME }
18
+ create_table(HMT_TABLE_NAME)
19
+ create_table(HMT_JOINS_TABLE_NAME) { |t| [TARGET_ASSOCIATION_NAME, HMT_ASSOCIATION_NAME].each &t.method(:references) }
20
+ create_table(POLY_TABLE_NAME) { |t| t.references(:polymorhicable); t.string(:polymorhicable_type) }
21
+ create_table(TARGET_TABLE_NAME) { |t| t.string :name; t.integer(:parent_id) }
22
+ create_table(HABTM_JOINS_TABLE_NAME, :id => false) { |t| [TARGET_ASSOCIATION_NAME, HABTM_ASSOCIATION_NAME].each &t.method(:references) }
23
+ end
24
+
25
+ def self.down
26
+ drop_table POLY_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(POLY_TABLE_NAME)
27
+ drop_table TARGET_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(TARGET_TABLE_NAME)
28
+ drop_table HABTM_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(HABTM_TABLE_NAME)
29
+ drop_table HM_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(HM_TABLE_NAME)
30
+ drop_table HMT_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(HMT_TABLE_NAME)
31
+ drop_table HO_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(HO_TABLE_NAME)
32
+ drop_table HMT_JOINS_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(HMT_JOINS_TABLE_NAME)
33
+ drop_table HABTM_JOINS_TABLE_NAME if ActiveRecord::Base.connection.tables.include?(HABTM_JOINS_TABLE_NAME)
34
+ end
35
+ end
36
+ TestMigration.up
37
+
38
+ class HasManyCacheable < ActiveRecord::Base
39
+ set_table_name HM_TABLE_NAME
40
+ belongs_to :cacher, :class_name => 'Cacher'
41
+ end
42
+
43
+ class HasManyThroughCacheable < ActiveRecord::Base
44
+ set_table_name HMT_TABLE_NAME
45
+
46
+ has_many :joins, :class_name => 'Join'
47
+ has_many :cachers, :through => :joins, :class_name => 'Cacher'
48
+ end
49
+
50
+ class Join < ActiveRecord::Base
51
+ set_table_name HMT_JOINS_TABLE_NAME
52
+
53
+ scope :test_scope, joins(:cacher).where(:cachers => { :name => %w{one two} })
54
+
55
+ belongs_to :cacher, :class_name => 'Cacher'
56
+ belongs_to :has_many_through_cacheable, :class_name => 'HasManyThroughCacheable'
57
+ end
58
+
59
+ class HasAndBelongsToManyCacheable < ActiveRecord::Base
60
+ set_table_name HABTM_TABLE_NAME
61
+ has_and_belongs_to_many :cachers, :class_name => 'Cacher'
62
+ end
63
+
64
+ class Polymorphic < ActiveRecord::Base
65
+ set_table_name POLY_TABLE_NAME
66
+ belongs_to :polymorhicable, :polymorphic => true
67
+ end
68
+
69
+ class Cacher < ActiveRecord::Base
70
+ cattr_accessor :stamp
71
+ set_table_name TARGET_TABLE_NAME
72
+
73
+ scope :test_scope, where(:name => %w{one two})
74
+
75
+ has_and_belongs_to_many :has_and_belongs_to_many_cacheables, :class_name => 'HasAndBelongsToManyCacheable'
76
+ has_many :has_many_cacheables, :class_name => 'HasManyCacheable'
77
+ has_many :joins, :class_name => 'Join'
78
+ has_many :has_many_through_cacheables, :through => :joins, :class_name => 'HasManyThroughCacheable'
79
+ has_many :polymorphics, :as => :polymorhicable
80
+ has_many :child_cachers, :class_name => 'Cacher', :foreign_key => 'parent_id', :primary_key => 'id'
81
+
82
+ def to_param; name end
83
+ end
@@ -0,0 +1,122 @@
1
+ require "spec_helper"
2
+
3
+ describe CacheMachine::Adapters::Redis do
4
+ subject { CacheMachine::Cache::map_adapter }
5
+
6
+ let(:cacher) { Cacher.create }
7
+
8
+ before :each do
9
+ CacheMachine::Cache::Mapper.new do
10
+ resource Cacher do
11
+ collection :has_many_through_cacheables
12
+ end
13
+ end
14
+ end
15
+
16
+ describe "#association_ids" do
17
+ let(:target) { Cacher.create(:name => 'foo') }
18
+ let(:hmt1) { target.has_many_through_cacheables.create }
19
+ let(:hmt2) { target.has_many_through_cacheables.create }
20
+
21
+ before :each do
22
+ hmt1 and hmt2
23
+ end
24
+
25
+ context "primary direction" do
26
+ after :each do
27
+ subject.association_ids(target, :has_many_through_cacheables).should =~ [hmt1.id, hmt2.id].map(&:to_s)
28
+ end
29
+
30
+ context "with clear cache" do
31
+ it("returns ids of an association") {}
32
+ end
33
+
34
+ context "filled cache" do
35
+ it "returns ids from cache if has already requested before" do
36
+ subject.association_ids(target, :has_many_through_cacheables)
37
+ subject.redis.should_receive(:smembers).and_return [hmt1.id, hmt2.id].map(&:to_s)
38
+ end
39
+ end
40
+ end
41
+
42
+ context "reverse direction" do
43
+ it "works" do
44
+ subject.association_ids(target.has_many_through_cacheables.first, :cachers).should == [target.id]
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "#reverse_association_ids" do
50
+ let(:target) { cacher.has_many_through_cacheables.create }
51
+
52
+ context "with clear cache" do
53
+ it "returns ids of an association" do
54
+ subject.reverse_association_ids(Cacher, :has_many_through_cacheables, target).should == [cacher.id.to_s]
55
+ end
56
+ end
57
+
58
+ context "filled cache" do
59
+ it "returns ids from cache if has already requested before" do
60
+ subject.reverse_association_ids(Cacher, :has_many_through_cacheables, target)
61
+ target.should_not_receive(:cache_map_ids)
62
+ subject.reverse_association_ids(Cacher, :has_many_through_cacheables, target)
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#append_id_to_map" do
68
+ let(:key) { subject.get_map_key(cacher, :joins) }
69
+
70
+ it "works" do
71
+ ::Redis.any_instance.should_receive(:sadd).with(key, 1)
72
+ subject.append_id_to_map(cacher, :joins, 1)
73
+ end
74
+ end
75
+
76
+ describe "#append_id_to_reverse_map" do
77
+ let(:join) { cacher.joins.create }
78
+ let(:key) { subject.get_reverse_map_key(Cacher, :joins, join) }
79
+
80
+ it "works" do
81
+ ::Redis.any_instance.should_receive(:sadd).with(key, 1)
82
+ subject.append_id_to_reverse_map(Cacher, :joins, join, 1)
83
+ end
84
+ end
85
+
86
+ describe "#fetch" do
87
+ let(:cache_key) { 'test' }
88
+ let(:key) { subject.get_content_key(cache_key) }
89
+
90
+ it "works" do
91
+ ::Redis.any_instance.should_receive(:get).with(key)
92
+ subject.fetch(cache_key)
93
+ end
94
+ end
95
+
96
+ describe "#delete" do
97
+ it "works" do
98
+ ::Redis.any_instance.should_receive(:del).with('test')
99
+ subject.delete('test')
100
+ end
101
+ end
102
+
103
+ describe "#delete_content" do
104
+ let(:content_key) { 'test' }
105
+ let(:key) { subject.get_content_key(content_key) }
106
+
107
+ it "works" do
108
+ ::Redis.any_instance.should_receive(:del).with(key)
109
+ subject.delete_content(content_key)
110
+ end
111
+ end
112
+
113
+ describe "#reset_timestamp" do
114
+ let(:timestamp) { 'test' }
115
+ let(:key) { subject.get_timestamp_key(timestamp) }
116
+
117
+ it "works" do
118
+ ::Redis.any_instance.should_receive(:del).with(key)
119
+ subject.reset_timestamp(timestamp)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,30 @@
1
+ require "active_record"
2
+ require "rails"
3
+ require "logger"
4
+ require "rspec"
5
+ require "rspec-rails"
6
+ require "cache-machine"
7
+ require "cache-machine-redis"
8
+
9
+ RSpec.configure { |config| config.mock_with :rspec }
10
+
11
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "db/sqlite3.test.db")
12
+
13
+ CacheMachine::Logger.level = :info
14
+
15
+ Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(:memory_store) # You can set memcached
16
+
17
+ url = "redis://zininserge:45fb4685efcf46c383d6938faca50885@carp.redistogo.com:9383/"
18
+ uri = URI.parse(url)
19
+ adapter = CacheMachine::Adapters::Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
20
+
21
+ CacheMachine::Cache::map_adapter = adapter
22
+ CacheMachine::Cache::storage_adapter = adapter
23
+ CacheMachine::Cache::timestamps_adapter = adapter
24
+
25
+ adapter.redis.flushdb
26
+
27
+ ActiveRecord::Base.logger = Logger.new(nil)
28
+ CacheMachine::Logger.logger = Logger.new(nil)
29
+
30
+ require 'fixtures'
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache-machine-redis
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sergei Zinin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: cache-machine
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 0
31
+ - 2
32
+ - 0
33
+ version: 0.2.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: redis
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 2
47
+ - 2
48
+ - 2
49
+ version: 2.2.2
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: bundler
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
65
+ version: 1.0.0
66
+ type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: rails
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 5
77
+ segments:
78
+ - 3
79
+ version: "3"
80
+ type: :development
81
+ version_requirements: *id004
82
+ description: Redis adapter for Cache Machine gem.
83
+ email: szinin@partyearth.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README.md
91
+ files:
92
+ - .gitignore
93
+ - .travis.yml
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - VERSION
100
+ - cache-machine-redis.gemspec
101
+ - db/.gitignore
102
+ - lib/cache-machine-redis.rb
103
+ - lib/cache_machine/adapters/redis.rb
104
+ - spec/fixtures.rb
105
+ - spec/lib/cache_machine/adapters/redis_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: http://github.com/zininserge/cache-machine-redis
108
+ licenses:
109
+ - MIT
110
+ post_install_message:
111
+ rdoc_options: []
112
+
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ requirements: []
134
+
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.10
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: Allows Cache Machine to deal with Redis.
140
+ test_files: []
141
+