kaching 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ require 'kaching'
2
+ require 'support/db/connection'
3
+ require 'support/db/models'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ # ==========================> Redis test configuration
8
+
9
+ root = File.dirname(__FILE__)
10
+ REDIS_PID = File.join(root, "tmp/pids/redis-test.pid")
11
+ REDIS_CACHE_PATH = File.join(root, "tmp/cache/")
12
+
13
+ FileUtils.mkdir_p File.join(root, "tmp/pids")
14
+ FileUtils.mkdir_p File.join(root, "tmp/cache")
15
+
16
+ config.before(:suite) do
17
+ redis_options = {
18
+ "daemonize" => 'yes',
19
+ "pidfile" => REDIS_PID,
20
+ "port" => 9726,
21
+ "timeout" => 300,
22
+ "save 900" => 1,
23
+ "save 300" => 1,
24
+ "save 60" => 10000,
25
+ "dbfilename" => "dump.rdb",
26
+ "dir" => REDIS_CACHE_PATH,
27
+ "loglevel" => "debug",
28
+ "logfile" => "stdout",
29
+ "databases" => 16
30
+ }.map { |k, v| "#{k} #{v}" }.join('\n')
31
+ cmd = "echo '#{redis_options}' | redis-server -"
32
+ system cmd
33
+
34
+
35
+ uri = URI.parse("http://localhost:9726")
36
+ $redis = Redis.new(host: uri.host, port: uri.port)
37
+
38
+ Kaching::StorageProviders.Redis = $redis
39
+ end
40
+
41
+ config.after(:suite) do
42
+ %x{
43
+ cat #{REDIS_PID} | xargs kill -QUIT
44
+ rm -f #{REDIS_CACHE_PATH}dump.rdb
45
+ rm -f #{REDIS_PID}
46
+ }
47
+ end
48
+
49
+ config.before(:each) do
50
+ $redis.flushdb
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
2
+ ActiveRecord::Base.establish_connection('sqlite3')
3
+
4
+ require 'support/db/schema'
@@ -0,0 +1,6 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ encoding: utf8
5
+ charset: utf8
6
+ timeout: 5000
@@ -0,0 +1,69 @@
1
+ class Article < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
4
+
5
+ class Follow < ActiveRecord::Base
6
+ belongs_to :user
7
+ belongs_to :item, polymorphic: true
8
+ end
9
+
10
+ class Car < ActiveRecord::Base
11
+ belongs_to :driver, class_name: 'User', foreign_key: 'driver_id'
12
+ end
13
+
14
+ class Movie < ActiveRecord::Base
15
+ end
16
+
17
+ class Like < ActiveRecord::Base
18
+ belongs_to :user
19
+ belongs_to :item, polymorphic: true
20
+ end
21
+
22
+ class UserMovie < ActiveRecord::Base
23
+ belongs_to :user
24
+ belongs_to :movie
25
+ end
26
+
27
+ class User < ActiveRecord::Base
28
+ has_many :articles
29
+
30
+ cache_counter :articles
31
+
32
+ has_many :cars, foreign_key: 'driver_id'
33
+
34
+ # cache_counter :following_users, class_name: 'Follow' do |user|
35
+ # Follow.where(user_id: user.id, item_type: 'User')
36
+ # end
37
+ #
38
+ # cache_counter :follower_users, class_name: 'Follow', foreign_key: 'item_id' do |user|
39
+ # Follow.where(item_id: user.id, item_type: 'User')
40
+ # end
41
+
42
+ cache_counter :cars
43
+
44
+ has_many :likes
45
+ has_many :user_movies
46
+ has_many :movies, through: :user_movies
47
+
48
+ cache_list :likes, polymorphic: true,
49
+ item_key: 'item',
50
+ add_method_name: 'like!',
51
+ remove_method_name: 'unlike!',
52
+ exists_method_name: 'likes?'
53
+
54
+ cache_list :user_movies, item_key: 'movie'
55
+
56
+ has_many :following_users, class_name: 'Follow', foreign_key: 'user_id', conditions: "item_type = 'User'"
57
+ has_many :follower_users, class_name: 'Follow', foreign_key: 'item_id', conditions: "item_type = 'User'"
58
+
59
+ cache_list :following_users, class_name: 'Follow',
60
+ item_key: 'item',
61
+ method_verb: 'follow',
62
+ polymorphic: true
63
+
64
+ cache_list :follower_users, class_name: 'Follow',
65
+ foreign_key: 'item_id',
66
+ add_alter_methods: false,
67
+ polymorphic: true
68
+
69
+ end
@@ -0,0 +1,38 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table :users do |t|
3
+ t.string :name
4
+ end
5
+
6
+ create_table :admins do |t|
7
+ t.string :name
8
+ end
9
+
10
+ create_table :movies do |t|
11
+ t.string :name
12
+ end
13
+
14
+ create_table :user_movies do |t|
15
+ t.belongs_to :user
16
+ t.belongs_to :movie
17
+ end
18
+
19
+ create_table :likes do |t|
20
+ t.belongs_to :user
21
+ t.belongs_to :item, polymorphic: true
22
+ end
23
+
24
+ create_table :articles do |t|
25
+ t.string :title
26
+ t.belongs_to :user
27
+ end
28
+
29
+ create_table :cars do |t|
30
+ t.string :model
31
+ t.belongs_to :driver
32
+ end
33
+
34
+ create_table :follows do |t|
35
+ t.belongs_to :user
36
+ t.belongs_to :item, polymorphic: true
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kaching
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Elad Ossadon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: redis
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
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'
78
+ - !ruby/object:Gem::Dependency
79
+ name: sqlite3
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: Doesn't hit the DB for counters and existence of a many-to-many association.
95
+ email:
96
+ - elad@ossadon.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - CHANGELOG.md
104
+ - Gemfile
105
+ - README.md
106
+ - Rakefile
107
+ - kaching.gemspec
108
+ - lib/kaching.rb
109
+ - lib/kaching/cache_counter.rb
110
+ - lib/kaching/cache_counter_onmethod.rb
111
+ - lib/kaching/cache_list.rb
112
+ - lib/kaching/model_additions.rb
113
+ - lib/kaching/railtie.rb
114
+ - lib/kaching/storage_providers/memory.rb
115
+ - lib/kaching/storage_providers/redis.rb
116
+ - lib/kaching/version.rb
117
+ - spec/lib/cache_counter_spec.rb
118
+ - spec/lib/cache_list_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/support/db/connection.rb
121
+ - spec/support/db/database.yml
122
+ - spec/support/db/models.rb
123
+ - spec/support/db/schema.rb
124
+ homepage: ''
125
+ licenses: []
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project: kaching
144
+ rubygems_version: 1.8.24
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Cache counters and lists of Rails ActiveRecord in an external storage such
148
+ as Redis.
149
+ test_files:
150
+ - spec/lib/cache_counter_spec.rb
151
+ - spec/lib/cache_list_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spec/support/db/connection.rb
154
+ - spec/support/db/database.yml
155
+ - spec/support/db/models.rb
156
+ - spec/support/db/schema.rb
157
+ has_rdoc: