CacheGorilla 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/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-07-10
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/CacheGorilla.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ spec/CacheGorilla_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
13
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,3 @@
1
+
2
+ For more information on CacheGorilla, see http://github.com/timrosenblatt/CacheGorilla/
3
+
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = CacheGorilla
2
+
3
+ http://www.animalpictures1.com/data/media/65/gorilla-7.jpg
4
+
5
+ * http://github.com/timrosenblatt/CacheGorilla
6
+
7
+ == DESCRIPTION:
8
+
9
+ Let's say you've got a MongoDB server, being used as a key-value store for an app being served by three sticky-load-balanced web servers. Running an instance of memcached will speed up repeated reads.
10
+
11
+ === tl;dr
12
+
13
+ This speeds up MongoDB by using antimatter and solar rays.
14
+
15
+ == FEATURES/PROBLEMS:
16
+
17
+ * FIX (list of features or problems)
18
+
19
+ == SYNOPSIS:
20
+
21
+ include CacheGorilla
22
+
23
+ @cg = CacheGorilla.new
24
+ @cg["key"] = "value"
25
+
26
+ == REQUIREMENTS:
27
+
28
+ * You need either the `memcached` or `memcache-client` gem, and the `mongo` gem
29
+
30
+ == INSTALL:
31
+
32
+ * FIX (sudo gem install, anything else)
33
+
34
+ == LICENSE:
35
+
36
+ (The MIT License)
37
+
38
+ Copyright (c) 2010 Tim Rosenblatt
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ 'Software'), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
54
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
55
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
56
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
57
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/CacheGorilla'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'CacheGorilla' do
14
+ self.developer 'Tim', 'Rosenblatt'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,100 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module CacheGorilla
5
+ VERSION = '0.0.1'
6
+
7
+ begin
8
+ require "memcached"
9
+ MemCache = Memcached
10
+ rescue LoadError
11
+ begin
12
+ require "memcache"
13
+ rescue LoadError
14
+ puts "You need either the `memcached` or `memcache-client` gem"
15
+ exit
16
+ end
17
+ rescue
18
+ puts "You need either the `memcached` or `memcache-client` gem"
19
+ exit
20
+ end
21
+
22
+ begin
23
+ require "mongo"
24
+ rescue LoadError
25
+ puts "You need the mongo gem"
26
+ exit
27
+ end
28
+
29
+ # This code is heavily inspired by Yehuda's Moneta (http://github.com/wycats/moneta)
30
+ class CacheGorilla
31
+ # :server sets up memcache, :host sets up mongo
32
+ def initialize(options = {})
33
+ @options = {
34
+ :host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
35
+ :port => ENV['MONGO_RUBY_DRIVER_PORT'] || 27017,
36
+ :db => 'cache',
37
+ :collection => 'cache',
38
+ :server => 'localhost'
39
+ }.update(options)
40
+
41
+ @mongo_connection = Mongo::Connection.new(@options[:host], @options[:port], :pool_size => 5, :timeout => 5)
42
+ @mongo_collection = @mongo_connection.db(@options[:db]).collection(@options[:collection])
43
+
44
+ @memcache = MemCache.new(options[:server], @options)
45
+ end
46
+
47
+ def key?(key)
48
+ !!self[key]
49
+ end
50
+
51
+ alias has_key? key?
52
+
53
+ def [](key)
54
+ begin
55
+ @memcache.get(key)
56
+ rescue Memcached::NotFound
57
+ res = @mongo_collection.find({'_id' => key}).first
58
+ res = nil if res && res['expires'] && Time.now > res['expires']
59
+
60
+ if res
61
+ args = [res['_id'], res['data'], res['expires']].compact
62
+ @memcache.set(*args)
63
+ end
64
+
65
+ res && res['data']
66
+ end
67
+ end
68
+
69
+ def []=(key, value)
70
+ store(key, value)
71
+ end
72
+
73
+ def delete(key)
74
+ # todo What's the best way to run these two calls at once, given that it's very much one-at-a-time
75
+ value = self[key]
76
+ @mongo_collection.remove('_id' => key) if value
77
+ @memcache.delete(key) if value
78
+ value
79
+ end
80
+
81
+ # Pass an option of :bypass_memcache if you want. Set the key to any value.
82
+ def store(key, value, options = {})
83
+ # todo What's the best way to run these two calls at once, given that it's very much one-at-a-time
84
+ exp = options[:expires_in] ? (Time.now + options[:expires_in]) : nil
85
+ @mongo_collection.update({ '_id' => key }, { '_id' => key, 'data' => value, 'expires' => exp }, { :upsert => true }) # upsert is the best technical term ever.
86
+
87
+ unless options.has_key?(:bypass_memcache)
88
+ args = [key, value, options[:expires_in]].compact
89
+ @memcache.set(*args)
90
+ end
91
+
92
+ value
93
+ end
94
+
95
+ def clear
96
+ @mongo_connection.drop_database(@options[:db])
97
+ @memcache.flush
98
+ end
99
+ end
100
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/CacheGorilla.rb'}"
9
+ puts "Loading CacheGorilla gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,102 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ # For sneaking a peek during testing
4
+ module CacheGorilla
5
+ class CacheGorilla
6
+ # attr_accessor :memcache, :mongo_collection
7
+
8
+ def mongo_get(key)
9
+ @mongo_collection.find({'_id' => key}).first['data']
10
+ end
11
+
12
+ def mongo_set(key, value)
13
+ @mongo_collection.update({ '_id' => key }, { '_id' => key, 'data' => value, 'expires' => nil }, { :upsert => true })
14
+ end
15
+
16
+ def memcache_get(key)
17
+ @memcache.get(key)
18
+ rescue Memcached::NotFound
19
+ nil
20
+ end
21
+
22
+ def memcache_set(key, value)
23
+ args = [key, value]
24
+ @memcache.set(*args)
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "CacheGorilla" do
30
+ include CacheGorilla
31
+
32
+ before(:each) do
33
+ @cg = CacheGorilla.new(:db => "cache_gorilla_test")
34
+ end
35
+
36
+ after(:each) do
37
+ @cg.clear
38
+ end
39
+
40
+ it "can set and get values" do
41
+ @cg.key?("Unicorns!").should be_false
42
+
43
+ @cg["Unicorns!"] = "Ponies!"
44
+
45
+ @cg["Unicorns!"].should == "Ponies!"
46
+ end
47
+
48
+ it "can check for the presence of a key" do
49
+ @cg.key?("Scrooge").should be_false
50
+
51
+ @cg["Scrooge"] = "Christmas Spirit"
52
+
53
+ @cg.key?("Scrooge").should be_true
54
+ end
55
+
56
+ it "can delete keys" do
57
+ @cg["Unicorns!"] = "Ponies!"
58
+ @cg.delete("Unicorns!")
59
+
60
+ @cg.key?("Unicorns!").should be_false
61
+ end
62
+
63
+ it "respects expirations" do
64
+ @cg.store("key", "value", { :expires_in => 5 })
65
+ sleep(6)
66
+ @cg["key"].should be_nil
67
+ end
68
+
69
+ it "fills memcache on cache misses" do
70
+ @cg.memcache_get("key").should be_nil
71
+ @cg.mongo_set("key", "value")
72
+
73
+ @cg["key"].should == "value"
74
+
75
+ @cg.memcache_get("key").should == "value"
76
+ end
77
+
78
+ it "returns nil when a key is not found" do
79
+ @cg["EasterBunny"].should be_nil
80
+ end
81
+
82
+ it "sets values in both memcache and mongo" do
83
+ @cg["key"] = "value"
84
+
85
+ @cg.mongo_get("key").should == "value"
86
+ @cg.memcache_get("key").should == "value"
87
+ end
88
+
89
+ it "caches" do
90
+ @cg["key"] = "value"
91
+ @cg.mongo_set("key", "not the value")
92
+
93
+ @cg["key"].should == "value"
94
+ end
95
+
96
+ it "can bypass memcache" do
97
+ @cg.store("key", "value", :bypass_memcache => true)
98
+
99
+ @cg.memcache_get("key").should be_nil
100
+ @cg["key"].should == "value"
101
+ end
102
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'CacheGorilla'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: CacheGorilla
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Tim
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-10 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 4
31
+ version: 2.0.4
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: hoe
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 6
44
+ - 1
45
+ version: 2.6.1
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: Let's say you've got a MongoDB server, being used as a key-value store for an app being served by three sticky-load-balanced web servers. Running an instance of memcached will speed up repeated reads.
49
+ email:
50
+ - Rosenblatt
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - History.txt
57
+ - Manifest.txt
58
+ - PostInstall.txt
59
+ files:
60
+ - History.txt
61
+ - Manifest.txt
62
+ - PostInstall.txt
63
+ - README.rdoc
64
+ - Rakefile
65
+ - lib/CacheGorilla.rb
66
+ - script/console
67
+ - script/destroy
68
+ - script/generate
69
+ - spec/CacheGorilla_spec.rb
70
+ - spec/spec.opts
71
+ - spec/spec_helper.rb
72
+ - tasks/rspec.rake
73
+ has_rdoc: true
74
+ homepage: http://www.animalpictures1.com/data/media/65/gorilla-7.jpg
75
+ licenses: []
76
+
77
+ post_install_message: PostInstall.txt
78
+ rdoc_options:
79
+ - --main
80
+ - README.rdoc
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project: CacheGorilla
100
+ rubygems_version: 1.3.6
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Let's say you've got a MongoDB server, being used as a key-value store for an app being served by three sticky-load-balanced web servers
104
+ test_files: []
105
+