spymemcached 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Golick
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,39 @@
1
+ = spymemcached
2
+
3
+ This gem (jruby only) is a memcached client that is a simple wrapper around the very fast spymemcached. It also contains a rails cache store that uses the spymemcached client.
4
+
5
+ = Install it
6
+
7
+ gem install spymemcached
8
+
9
+ Or with bundler:
10
+
11
+ gem "spymemcached", ">=0.1.0"
12
+
13
+ = How to use the cache store
14
+
15
+ config.cache_store :spymemcached_store, "memcache01:11211", "memcached02:11211", ...
16
+
17
+ = Or use the client directly:
18
+
19
+ require "spymemcached"
20
+
21
+ cache = Spymemcached.new("memcache01:11211", "memcache02:11211")
22
+ cache.set("a", "b")
23
+ cache.get("a") #=> "b"
24
+
25
+ # ... etc ...
26
+
27
+ == Note on Patches/Pull Requests
28
+
29
+ * Fork the project.
30
+ * Make your feature addition or bug fix.
31
+ * Add tests for it. This is important so I don't break it in a
32
+ future version unintentionally.
33
+ * Commit, do not mess with rakefile, version, or history.
34
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
35
+ * Send me a pull request. Bonus points for topic branches.
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2010 James Golick. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "spymemcached"
8
+ gem.summary = %Q{A jruby-only memcached client and rails cache store that uses spymemcached under the hood.}
9
+ gem.description = %Q{A jruby-only memcached client and rails cache store that uses spymemcached under the hood.}
10
+ gem.email = "jamesgolick@gmail.com"
11
+ gem.homepage = "http://github.com/jamesgolick/spymemcached"
12
+ gem.authors = ["James Golick"]
13
+ gem.platform = "java"
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "spymemcached #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,67 @@
1
+ require "active_support"
2
+ require "spymemcached"
3
+
4
+ module ActiveSupport
5
+ module Cache
6
+ class SpymemcachedStore < Store
7
+ attr_reader :addresses
8
+
9
+ def initialize(*addresses)
10
+ addresses.flatten!
11
+
12
+ @addresses = addresses
13
+ @cache = Spymemcached.new(@addresses)
14
+ end
15
+
16
+ def read(key, options = nil)
17
+ super
18
+ @cache.get(key)
19
+ end
20
+
21
+ # Set the key to the given value. Pass :unless_exist => true if you want to
22
+ # skip setting a key that already exists.
23
+ def write(key, value, options = nil)
24
+ super
25
+ method = unless_exist?(options) ? :add : :set
26
+ @cache.send(method, key, value, expiry(options))
27
+ end
28
+
29
+ def delete(key, options = nil)
30
+ super
31
+ @cache.del(key)
32
+ end
33
+
34
+ def exist?(key, options = nil)
35
+ !read(key, options).nil?
36
+ end
37
+
38
+ def increment(key, amount=1)
39
+ log 'incrementing', key, amount
40
+ @cache.incr(key, amount)
41
+ end
42
+
43
+ def decrement(key, amount=1)
44
+ log 'decrementing', key, amount
45
+ @cache.decr(key, amount)
46
+ end
47
+
48
+ def delete_matched(matcher, options = nil)
49
+ super
50
+ raise NotImplementedError
51
+ end
52
+
53
+ def clear
54
+ @cache.flush
55
+ end
56
+
57
+ private
58
+ def unless_exist?(options)
59
+ options.respond_to?(:[]) && options[:unless_exist]
60
+ end
61
+
62
+ def expiry(options)
63
+ options.respond_to?(:[]) && options[:expires_in] ? options[:expires_in] : 0
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,54 @@
1
+ require "java"
2
+ require "spymemcached/memcached-2.5.jar"
3
+
4
+ class Spymemcached
5
+ java_import "net.spy.memcached.MemcachedClient"
6
+ java_import "java.net.InetSocketAddress"
7
+
8
+ def initialize(servers)
9
+ @client = MemcachedClient.new(servers.map do |s|
10
+ host, port = s.split(":")
11
+ InetSocketAddress.new(host, port.to_i)
12
+ end)
13
+ end
14
+
15
+ def set(key, value, expiration = 0)
16
+ @client.set(key, expiration, value)
17
+ end
18
+
19
+ def get(key)
20
+ @client.get(key)
21
+ end
22
+
23
+ def incr(key, by = 1)
24
+ @client.incr(key, by)
25
+ end
26
+
27
+ def decr(key, by = 1)
28
+ @client.decr(key, by)
29
+ end
30
+
31
+ def append(key, value)
32
+ @client.append(0, key, value).get
33
+ end
34
+
35
+ def prepend(key, value)
36
+ @client.prepend(0, key, value).get
37
+ end
38
+
39
+ def multiget(*keys)
40
+ Hash[*@client.getBulk(*keys).map { |k,v| [k,v] }.flatten]
41
+ end
42
+
43
+ def add(key, value, expiration = 0)
44
+ @client.add(key, expiration, value).get
45
+ end
46
+
47
+ def del(key)
48
+ @client.delete(key).get
49
+ end
50
+
51
+ def flush
52
+ @client.flush
53
+ end
54
+ end
Binary file
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'spymemcached'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,103 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Spymemcached do
4
+ before do
5
+ @cache = Spymemcached.new(["localhost:11211"])
6
+ end
7
+ after { @cache.flush }
8
+
9
+ it "sets and gets keys" do
10
+ @cache.set("a", "b")
11
+ @cache.get("a").should == "b"
12
+ end
13
+
14
+ it "returns nil for missing keys" do
15
+ @cache.get("asdf").should be_nil
16
+ end
17
+
18
+ it "sets expiration on keys" do
19
+ @cache.set("a", "b", 1)
20
+ sleep(2)
21
+ @cache.get("a").should be_nil
22
+ end
23
+
24
+ it "increments keys" do
25
+ @cache.set("number", "1")
26
+ @cache.incr("number")
27
+ @cache.get("number").should == "2"
28
+ end
29
+
30
+ it "increments keys by a set amount" do
31
+ @cache.set("number", "1")
32
+ @cache.incr("number", 2)
33
+ @cache.get("number").should == "3"
34
+ end
35
+
36
+ it "decrements keys" do
37
+ @cache.set("number", "2")
38
+ @cache.decr("number")
39
+ @cache.get("number").should == "1"
40
+ end
41
+
42
+ it "decrements keys by a set amount" do
43
+ @cache.set("number", "2")
44
+ @cache.decr("number", 2)
45
+ @cache.get("number").should == "0"
46
+ end
47
+
48
+ it "appends to keys" do
49
+ @cache.set("appendtome", "a")
50
+ @cache.append("appendtome", "b")
51
+ @cache.get("appendtome").should == "ab"
52
+ end
53
+
54
+ it "prepends to keys" do
55
+ @cache.set("prependtome", "b")
56
+ @cache.prepend("prependtome", "a")
57
+ @cache.get("prependtome").should == "ab"
58
+ end
59
+
60
+ it "returns boolean for prepend" do
61
+ @cache.set("prependtome", "b")
62
+ @cache.prepend("prependtome", "a").should == true
63
+ end
64
+
65
+ it "returns boolean for append" do
66
+ @cache.set("appendtome", "b")
67
+ @cache.append("appendtome", "a").should == true
68
+ end
69
+
70
+ it "multigets" do
71
+ @cache.set("a", "b")
72
+ @cache.set("b", "c")
73
+ @cache.set("c", "d")
74
+ @cache.multiget("a", "b", "c").should == {
75
+ "a" => "b",
76
+ "b" => "c",
77
+ "c" => "d"
78
+ }
79
+ end
80
+
81
+ it "supports add" do
82
+ @cache.add("a", "b").should == true
83
+ @cache.get("a").should == "b"
84
+ @cache.add("a", "b").should == false
85
+ end
86
+
87
+ it "supports expiry for add" do
88
+ @cache.add("a", "b", 1).should == true
89
+ sleep(2)
90
+ @cache.get("a").should be_nil
91
+ end
92
+
93
+ it "supports deleting keys" do
94
+ @cache.set("a", "b")
95
+ @cache.del("a")
96
+ @cache.get("a").should be_nil
97
+ end
98
+
99
+ it "returns boolean for deletes" do
100
+ @cache.set("a", "b")
101
+ @cache.del("a").should == true
102
+ end
103
+ end
@@ -0,0 +1,62 @@
1
+ require "spec"
2
+ require "active_support/cache/spymemcached_store"
3
+
4
+ describe "SpymemcachedStore" do
5
+ before do
6
+ @cache = ActiveSupport::Cache::SpymemcachedStore.new("localhost:11211")
7
+ end
8
+ after { @cache.clear }
9
+
10
+ it "writes to and reads from cache" do
11
+ @cache.write("a", "b")
12
+ @cache.read("a")
13
+ end
14
+
15
+ it "supports expiry on write" do
16
+ @cache.write("a", "b", :expires_in => 1)
17
+ sleep(2)
18
+ @cache.read("a").should be_nil
19
+ end
20
+
21
+ it "supports add via the unless_exist property" do
22
+ @cache.write("a", "b")
23
+ @cache.write("a", "c", :unless_exist => true)
24
+ @cache.read("a").should == "b"
25
+ end
26
+
27
+ it "supports deleting keys" do
28
+ @cache.write("a", "b")
29
+ @cache.delete("a")
30
+ @cache.read("a").should be_nil
31
+ end
32
+
33
+ it "supports exist?" do
34
+ @cache.exist?("a").should == false
35
+ @cache.write("a", "b")
36
+ @cache.exist?("a").should == true
37
+ end
38
+
39
+ it "supports increment" do
40
+ @cache.write("a", "1")
41
+ @cache.increment("a")
42
+ @cache.read("a").should == "2"
43
+ end
44
+
45
+ it "supports incrementing by a specific amount" do
46
+ @cache.write("a", "1")
47
+ @cache.increment("a", 2)
48
+ @cache.read("a").should == "3"
49
+ end
50
+
51
+ it "supports decrement" do
52
+ @cache.write("a", "1")
53
+ @cache.decrement("a")
54
+ @cache.read("a").should == "0"
55
+ end
56
+
57
+ it "supports decrementing by a specific amount" do
58
+ @cache.write("a", "2")
59
+ @cache.decrement("a", 2)
60
+ @cache.read("a").should == "0"
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{spymemcached}
8
+ s.version = "0.1.0"
9
+ s.platform = %q{java}
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["James Golick"]
13
+ s.date = %q{2010-09-27}
14
+ s.description = %q{A jruby-only memcached client and rails cache store that uses spymemcached under the hood.}
15
+ s.email = %q{jamesgolick@gmail.com}
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/active_support/cache/spymemcached_store.rb",
28
+ "lib/spymemcached.rb",
29
+ "lib/spymemcached/memcached-2.5.jar",
30
+ "spec/spec.opts",
31
+ "spec/spec_helper.rb",
32
+ "spec/spymemcached_spec.rb",
33
+ "spec/spymemcached_store_spec.rb",
34
+ "spymemcached.gemspec"
35
+ ]
36
+ s.homepage = %q{http://github.com/jamesgolick/spymemcached}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{A jruby-only memcached client and rails cache store that uses spymemcached under the hood.}
41
+ s.test_files = [
42
+ "spec/spec_helper.rb",
43
+ "spec/spymemcached_spec.rb",
44
+ "spec/spymemcached_store_spec.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ end
59
+ end
60
+
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spymemcached
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: java
11
+ authors:
12
+ - James Golick
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-27 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: A jruby-only memcached client and rails cache store that uses spymemcached under the hood.
36
+ email: jamesgolick@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/active_support/cache/spymemcached_store.rb
52
+ - lib/spymemcached.rb
53
+ - lib/spymemcached/memcached-2.5.jar
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ - spec/spymemcached_spec.rb
57
+ - spec/spymemcached_store_spec.rb
58
+ - spymemcached.gemspec
59
+ has_rdoc: true
60
+ homepage: http://github.com/jamesgolick/spymemcached
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A jruby-only memcached client and rails cache store that uses spymemcached under the hood.
91
+ test_files:
92
+ - spec/spec_helper.rb
93
+ - spec/spymemcached_spec.rb
94
+ - spec/spymemcached_store_spec.rb