adapter-memcached 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +38 -0
- data/LICENSE +19 -0
- data/README.rdoc +13 -0
- data/Rakefile +7 -0
- data/adapter-memcached.gemspec +22 -0
- data/examples/memcached.rb +25 -0
- data/lib/adapter/memcached/version.rb +5 -0
- data/lib/adapter/memcached.rb +56 -0
- data/log/test.log +1 -0
- data/spec/helper.rb +26 -0
- data/spec/memcached_spec.rb +62 -0
- metadata +110 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in adapter-memcached.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group(:development) do
|
7
|
+
gem 'rspec', '~> 2.3'
|
8
|
+
gem 'log_buddy', '~> 0.5.0'
|
9
|
+
gem 'timecop', '~> 0.3.5'
|
10
|
+
gem 'i18n', '0.5.0'
|
11
|
+
gem 'activesupport', '~> 3', :require => 'active_support'
|
12
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
adapter-memcached (1.0)
|
5
|
+
adapter (~> 0.5)
|
6
|
+
memcached (~> 1.0.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (3.0.3)
|
12
|
+
adapter (0.5.1)
|
13
|
+
diff-lcs (1.1.2)
|
14
|
+
i18n (0.5.0)
|
15
|
+
log_buddy (0.5.0)
|
16
|
+
memcached (1.0.2)
|
17
|
+
rspec (2.4.0)
|
18
|
+
rspec-core (~> 2.4.0)
|
19
|
+
rspec-expectations (~> 2.4.0)
|
20
|
+
rspec-mocks (~> 2.4.0)
|
21
|
+
rspec-core (2.4.0)
|
22
|
+
rspec-expectations (2.4.0)
|
23
|
+
diff-lcs (~> 1.1.2)
|
24
|
+
rspec-mocks (2.4.0)
|
25
|
+
timecop (0.3.5)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
activesupport (~> 3)
|
32
|
+
adapter (~> 0.5)
|
33
|
+
adapter-memcached!
|
34
|
+
i18n (= 0.5.0)
|
35
|
+
log_buddy (~> 0.5.0)
|
36
|
+
memcached (~> 1.0.2)
|
37
|
+
rspec (~> 2.3)
|
38
|
+
timecop (~> 0.3.5)
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Newtoy, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
= adapter-memcached
|
2
|
+
|
3
|
+
Memcached adapter for adapter gem.
|
4
|
+
|
5
|
+
See examples/ or specs/ for usage.
|
6
|
+
|
7
|
+
== Note on Patches/Pull Requests
|
8
|
+
|
9
|
+
* Fork the project.
|
10
|
+
* Make your feature addition or bug fix.
|
11
|
+
* Add tests for it. This is important so we don't break it in a future version unintentionally.
|
12
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine, but bump version in a commit by itself so we can ignore when we pull)
|
13
|
+
* Send us a pull request. Bonus points for topic branches.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "adapter/memcached/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "adapter-memcached"
|
7
|
+
s.version = Adapter::Memcached::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["John Nunemaker"]
|
10
|
+
s.email = ["nunemaker@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Adapter for memcached}
|
13
|
+
s.description = %q{Adapter for memcached}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'adapter', '~> 0.5'
|
21
|
+
s.add_dependency 'memcached', '~> 1.0.2'
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
root_path = Pathname(__FILE__).dirname.join('..').expand_path
|
5
|
+
lib_path = root_path.join('lib')
|
6
|
+
$:.unshift(lib_path)
|
7
|
+
|
8
|
+
require 'adapter/memcached'
|
9
|
+
|
10
|
+
client = Memcached.new('localhost:11211', :namespace => 'adapter_example')
|
11
|
+
adapter = Adapter[:memcached].new(client)
|
12
|
+
adapter.clear
|
13
|
+
|
14
|
+
adapter.write('foo', 'bar')
|
15
|
+
puts 'Should be bar: ' + adapter.read('foo').inspect
|
16
|
+
|
17
|
+
adapter.delete('foo')
|
18
|
+
puts 'Should be nil: ' + adapter.read('foo').inspect
|
19
|
+
|
20
|
+
adapter.write('foo', 'bar')
|
21
|
+
adapter.clear
|
22
|
+
puts 'Should be nil: ' + adapter.read('foo').inspect
|
23
|
+
|
24
|
+
puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
|
25
|
+
puts 'Should be bar: ' + adapter.read('foo')
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'adapter'
|
2
|
+
require 'memcached'
|
3
|
+
|
4
|
+
module Adapter
|
5
|
+
module Memcached
|
6
|
+
def read(key)
|
7
|
+
decode(client.get(key_for(key)))
|
8
|
+
rescue ::Memcached::NotFound
|
9
|
+
end
|
10
|
+
|
11
|
+
def write(key, value)
|
12
|
+
client.set(key_for(key), encode(value))
|
13
|
+
end
|
14
|
+
|
15
|
+
def delete(key)
|
16
|
+
read(key).tap { client.delete(key_for(key)) }
|
17
|
+
rescue ::Memcached::NotFound
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear
|
21
|
+
client.flush
|
22
|
+
end
|
23
|
+
|
24
|
+
def lock(name, options={}, &block)
|
25
|
+
key = name.to_s
|
26
|
+
start = Time.now
|
27
|
+
lock_acquired = false
|
28
|
+
expiration = options.fetch(:expiration, 1)
|
29
|
+
timeout = options.fetch(:timeout, 5)
|
30
|
+
|
31
|
+
while (Time.now - start) < timeout
|
32
|
+
begin
|
33
|
+
client.add(key, 'locked', expiration)
|
34
|
+
lock_acquired = true
|
35
|
+
break
|
36
|
+
rescue ::Memcached::NotStored
|
37
|
+
sleep 0.1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
raise(Adapter::LockTimeout.new(name, timeout)) unless lock_acquired
|
42
|
+
|
43
|
+
begin
|
44
|
+
yield
|
45
|
+
ensure
|
46
|
+
delete(key)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def key_for(key)
|
51
|
+
[super].pack("m").strip
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Adapter.define(:memcached, Adapter::Memcached)
|
data/log/test.log
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Logfile created on Tue Jan 25 21:59:54 -0500 2011 by logger.rb
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
|
6
|
+
Bundler.require(:default, :development)
|
7
|
+
|
8
|
+
require 'pathname'
|
9
|
+
require 'logger'
|
10
|
+
|
11
|
+
root_path = Pathname(__FILE__).dirname.join('..').expand_path
|
12
|
+
lib_path = root_path.join('lib')
|
13
|
+
log_path = root_path.join('log')
|
14
|
+
log_path.mkpath
|
15
|
+
|
16
|
+
require 'adapter/spec/an_adapter'
|
17
|
+
require 'adapter/spec/marshal_adapter'
|
18
|
+
require 'adapter/spec/json_adapter'
|
19
|
+
require 'adapter/spec/types'
|
20
|
+
|
21
|
+
logger = Logger.new(log_path.join('test.log'))
|
22
|
+
LogBuddy.init(:logger => logger)
|
23
|
+
|
24
|
+
Rspec.configure do |c|
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'adapter/memcached'
|
3
|
+
|
4
|
+
describe "Memcached adapter" do
|
5
|
+
before do
|
6
|
+
@client = Memcached.new('localhost:11211', :namespace => 'adapter_spec')
|
7
|
+
@adapter = Adapter[:memcached].new(@client)
|
8
|
+
@adapter.clear
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:adapter) { @adapter }
|
12
|
+
let(:client) { @client }
|
13
|
+
|
14
|
+
it_should_behave_like 'a marshaled adapter'
|
15
|
+
|
16
|
+
describe "#lock" do
|
17
|
+
let(:lock_key) { :add_game }
|
18
|
+
let(:lock_value) { 'locked' }
|
19
|
+
|
20
|
+
it "defaults expiration to 1" do
|
21
|
+
client.should_receive(:add).with(lock_key.to_s, lock_value, 1)
|
22
|
+
adapter.lock(lock_key) { }
|
23
|
+
end
|
24
|
+
|
25
|
+
it "allows setting expiration" do
|
26
|
+
client.should_receive(:add).with(lock_key.to_s, lock_value, 5)
|
27
|
+
adapter.lock(lock_key, :expiration => 5) { }
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "with no existing lock" do
|
31
|
+
it "acquires lock, performs block, and clears lock" do
|
32
|
+
result = false
|
33
|
+
adapter.lock(lock_key) { result = true }
|
34
|
+
|
35
|
+
result.should be_true
|
36
|
+
adapter.read(lock_key).should be_nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "with lock set" do
|
41
|
+
it "waits for unlock, performs block, and clears lock" do
|
42
|
+
result = false
|
43
|
+
client.add(lock_key.to_s, lock_value, 1)
|
44
|
+
adapter.lock(lock_key, :timeout => 2) { result = true }
|
45
|
+
|
46
|
+
result.should be_true
|
47
|
+
adapter.read(lock_key).should be_nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "with lock set that does not expire before timeout" do
|
52
|
+
it "raises lock timeout error" do
|
53
|
+
result = false
|
54
|
+
client.add(lock_key.to_s, lock_value, 2)
|
55
|
+
|
56
|
+
lambda do
|
57
|
+
adapter.lock(lock_key, :timeout => 1) { result = true }
|
58
|
+
end.should raise_error(Adapter::LockTimeout, 'Timeout on lock add_game exceeded 1 sec')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: adapter-memcached
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 1
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 5
|
9
|
+
version: "0.5"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- John Nunemaker
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-25 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: adapter
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 1
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 5
|
32
|
+
version: "0.5"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: memcached
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 19
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 2
|
48
|
+
version: 1.0.2
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Adapter for memcached
|
52
|
+
email:
|
53
|
+
- nunemaker@gmail.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- Gemfile.lock
|
64
|
+
- LICENSE
|
65
|
+
- README.rdoc
|
66
|
+
- Rakefile
|
67
|
+
- adapter-memcached.gemspec
|
68
|
+
- examples/memcached.rb
|
69
|
+
- lib/adapter/memcached.rb
|
70
|
+
- lib/adapter/memcached/version.rb
|
71
|
+
- log/test.log
|
72
|
+
- spec/helper.rb
|
73
|
+
- spec/memcached_spec.rb
|
74
|
+
has_rdoc: true
|
75
|
+
homepage: ""
|
76
|
+
licenses: []
|
77
|
+
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.7
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Adapter for memcached
|
108
|
+
test_files:
|
109
|
+
- spec/helper.rb
|
110
|
+
- spec/memcached_spec.rb
|