iron_cache 0.0.4 → 1.0.0
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/Gemfile.lock +13 -9
- data/README.md +21 -20
- data/Rakefile +0 -20
- data/iron_cache.gemspec +2 -1
- data/lib/iron_cache/caches.rb +45 -23
- data/lib/iron_cache/client.rb +7 -9
- data/lib/iron_cache/items.rb +17 -6
- data/lib/iron_cache/version.rb +1 -1
- data/test/quick_run.rb +0 -1
- data/test/test_base.rb +0 -1
- data/test/test_iron_cache.rb +105 -0
- metadata +20 -4
data/Gemfile.lock
CHANGED
@@ -1,26 +1,29 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
iron_cache (0.0
|
5
|
-
iron_core
|
4
|
+
iron_cache (0.1.0)
|
5
|
+
iron_core (>= 0.1.4)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
10
|
+
ffi (1.0.11)
|
11
|
+
iron_core (0.1.4)
|
11
12
|
bundler (> 1.0.0)
|
12
13
|
rest
|
13
14
|
rest-client
|
14
15
|
memcache-client (1.8.5)
|
15
|
-
mime-types (1.
|
16
|
+
mime-types (1.19)
|
16
17
|
rake (0.9.2.2)
|
17
|
-
rest (0.
|
18
|
-
rest-client
|
19
|
-
rest-client
|
18
|
+
rest (1.0.0)
|
19
|
+
rest-client (>= 0.3.0)
|
20
20
|
rest-client (1.6.7)
|
21
21
|
mime-types (>= 1.16)
|
22
|
-
test-unit (2.
|
23
|
-
|
22
|
+
test-unit (2.5.0)
|
23
|
+
typhoeus (0.4.2)
|
24
|
+
ffi (~> 1.0)
|
25
|
+
mime-types (~> 1.18)
|
26
|
+
uber_config (0.0.3)
|
24
27
|
|
25
28
|
PLATFORMS
|
26
29
|
ruby
|
@@ -30,4 +33,5 @@ DEPENDENCIES
|
|
30
33
|
memcache-client
|
31
34
|
rake
|
32
35
|
test-unit
|
36
|
+
typhoeus
|
33
37
|
uber_config
|
data/README.md
CHANGED
@@ -1,52 +1,53 @@
|
|
1
|
-
|
1
|
+
IronCache Ruby Client
|
2
2
|
-------------
|
3
3
|
|
4
4
|
Getting Started
|
5
5
|
==============
|
6
6
|
|
7
|
-
Install the gem:
|
7
|
+
1. Install the gem:
|
8
8
|
|
9
9
|
gem install iron_cache
|
10
10
|
|
11
|
-
|
11
|
+
2. Setup your Iron.io credentials: http://dev.iron.io/articles/configuration/
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
You can get your `token` and `project_id` at http://www.iron.io .
|
13
|
+
3. Create an IronCache client object:
|
16
14
|
|
15
|
+
@client = IronCache::Client.new
|
17
16
|
|
18
17
|
The Basics
|
19
18
|
=========
|
20
19
|
|
20
|
+
**Get a cache object**
|
21
|
+
|
22
|
+
You can have as many caches as you want, each with their own unique set of items.
|
23
|
+
|
24
|
+
@cache = @client.cache("my_cache")
|
25
|
+
|
26
|
+
Now you can use it:
|
27
|
+
|
21
28
|
**Put** an item in the cache:
|
22
29
|
|
23
|
-
msg = @
|
30
|
+
msg = @cache.put("mykey", "hello world!")
|
24
31
|
p msg
|
25
32
|
|
26
33
|
**Get** an item from the cache:
|
27
34
|
|
28
|
-
msg = @
|
35
|
+
msg = @cache.get("mykey")
|
29
36
|
p msg
|
30
37
|
|
31
38
|
**Delete** an item from the cache:
|
32
39
|
|
33
|
-
res = msg.delete # or @
|
40
|
+
res = msg.delete # or @cache.delete("mykey")
|
34
41
|
p res
|
35
42
|
|
43
|
+
**Increment** an item in the cache:
|
36
44
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
One of the following:
|
41
|
-
|
42
|
-
1. Pass `:cache_name=>'my_cache'` into IronCache::Client.new
|
43
|
-
1. `@iron_cache.cache_name = 'my_cache'`
|
44
|
-
1. Pass `:cache_name=>'my_cache'` into any post(), get(), or delete()
|
45
|
+
msg = @cache.increment("mycounter", 1)
|
46
|
+
p res
|
45
47
|
|
46
48
|
Cache Information
|
47
49
|
=================
|
48
50
|
|
49
|
-
cache = @iron_cache.
|
50
|
-
puts "
|
51
|
+
cache = @iron_cache.cache("my_cache")
|
52
|
+
puts "name: #{cache.name}"
|
51
53
|
|
52
|
-
|
data/Rakefile
CHANGED
@@ -1,26 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
|
4
|
-
begin
|
5
|
-
require 'jeweler2'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = "iron_cache"
|
8
|
-
gem.summary = "Ruby client for IronCache by www.iron.io"
|
9
|
-
gem.description = "Ruby client for IronCache by www.iron.io"
|
10
|
-
gem.email = "travis@iron.io"
|
11
|
-
gem.homepage = "http://www.iron.io"
|
12
|
-
gem.authors = ["Travis Reeder"]
|
13
|
-
gem.add_dependency 'iron_core'
|
14
|
-
gem.add_dependency 'rest-client'
|
15
|
-
gem.add_dependency 'rest', '>= 0.1.2'
|
16
|
-
#gem.add_dependency 'typhoeus'
|
17
|
-
gem.required_ruby_version = '>= 1.9'
|
18
|
-
end
|
19
|
-
Jeweler::GemcutterTasks.new
|
20
|
-
rescue LoadError
|
21
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler2"
|
22
|
-
end
|
23
|
-
|
24
4
|
require 'rake/testtask'
|
25
5
|
Rake::TestTask.new(:test) do |test|
|
26
6
|
test.libs << 'lib' << 'test'
|
data/iron_cache.gemspec
CHANGED
@@ -16,12 +16,13 @@ Gem::Specification.new do |gem|
|
|
16
16
|
|
17
17
|
gem.required_rubygems_version = ">= 1.3.6"
|
18
18
|
gem.required_ruby_version = Gem::Requirement.new(">= 1.9")
|
19
|
-
gem.add_runtime_dependency "iron_core"
|
19
|
+
gem.add_runtime_dependency "iron_core", ">= 0.1.4"
|
20
20
|
|
21
21
|
gem.add_development_dependency "test-unit"
|
22
22
|
gem.add_development_dependency "rake"
|
23
23
|
gem.add_development_dependency "memcache-client"
|
24
24
|
gem.add_development_dependency "uber_config"
|
25
|
+
gem.add_development_dependency "typhoeus"
|
25
26
|
|
26
27
|
|
27
28
|
end
|
data/lib/iron_cache/caches.rb
CHANGED
@@ -11,17 +11,21 @@ module IronCache
|
|
11
11
|
|
12
12
|
def path(options={})
|
13
13
|
path = "projects/#{@client.project_id}/caches"
|
14
|
+
if options[:name]
|
15
|
+
path << "/#{URI.escape(options[:name])}"
|
16
|
+
end
|
17
|
+
path
|
14
18
|
end
|
15
19
|
|
16
20
|
def list(options={})
|
17
21
|
ret = []
|
18
22
|
res = @client.get("#{path(options)}", options)
|
19
|
-
|
23
|
+
@client.logger.debug res.inspect
|
20
24
|
parsed = @client.parse_response(res, true)
|
21
|
-
|
25
|
+
@client.logger.debug parsed.inspect
|
22
26
|
parsed.each do |q|
|
23
|
-
|
24
|
-
q = Cache.new(
|
27
|
+
@client.logger.debug "cache: " + q.inspect
|
28
|
+
q = Cache.new(@client, q)
|
25
29
|
ret << q
|
26
30
|
end
|
27
31
|
ret
|
@@ -30,8 +34,12 @@ module IronCache
|
|
30
34
|
# options:
|
31
35
|
# :name => can specify an alternative queue name
|
32
36
|
def get(options={})
|
33
|
-
|
34
|
-
|
37
|
+
if options.is_a?(String)
|
38
|
+
options = {:name=>options}
|
39
|
+
end
|
40
|
+
options[:name] ||= @client.cache_name
|
41
|
+
res = @client.parse_response(@client.get("#{path(options)}"))
|
42
|
+
Cache.new(@client, res)
|
35
43
|
end
|
36
44
|
|
37
45
|
|
@@ -39,8 +47,8 @@ module IronCache
|
|
39
47
|
|
40
48
|
class Cache
|
41
49
|
|
42
|
-
def initialize(
|
43
|
-
@
|
50
|
+
def initialize(client, res)
|
51
|
+
@client = client
|
44
52
|
@data = res
|
45
53
|
end
|
46
54
|
|
@@ -60,25 +68,39 @@ module IronCache
|
|
60
68
|
raw["name"]
|
61
69
|
end
|
62
70
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
@
|
68
|
-
|
71
|
+
# Used if lazy loading
|
72
|
+
def load_cache
|
73
|
+
q = @client.caches.get(:name=>name)
|
74
|
+
@client.logger.debug "GOT Q: " + q.inspect
|
75
|
+
@data = q.raw
|
76
|
+
q
|
77
|
+
end
|
78
|
+
|
79
|
+
# not supported in API yet
|
80
|
+
#def size
|
81
|
+
# return raw["size"] if raw["size"]
|
82
|
+
# return @size if @size
|
83
|
+
# q = load_cache()
|
84
|
+
# @size = q.size
|
85
|
+
# @size
|
86
|
+
#end
|
87
|
+
|
88
|
+
def put(k, v, options={})
|
89
|
+
@client.items.put(k, v, options.merge(:cache_name=>name))
|
90
|
+
end
|
91
|
+
|
92
|
+
def get(k, options={})
|
93
|
+
@client.items.get(k, options.merge(:cache_name=>name))
|
94
|
+
end
|
95
|
+
|
96
|
+
def delete(k, options={})
|
97
|
+
@client.items.delete(k, options.merge(:cache_name=>name))
|
69
98
|
end
|
70
99
|
|
71
|
-
def
|
72
|
-
|
73
|
-
return @total_messages if @total_messages
|
74
|
-
q = @queues.get(:name=>name)
|
75
|
-
@total_messages = q.total_messages
|
76
|
-
@total_messages
|
100
|
+
def increment(k, amount=1, options={})
|
101
|
+
@client.items.increment(k, amount, options.merge(:cache_name=>name))
|
77
102
|
end
|
78
103
|
|
79
|
-
# def delete
|
80
|
-
# @messages.delete(self.id)
|
81
|
-
# end
|
82
104
|
end
|
83
105
|
|
84
106
|
end
|
data/lib/iron_cache/client.rb
CHANGED
@@ -9,27 +9,25 @@ module IronCache
|
|
9
9
|
|
10
10
|
AWS_US_EAST_HOST = "cache-aws-us-east-1.iron.io"
|
11
11
|
|
12
|
-
def self.version
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
12
|
attr_accessor :cache_name, :logger
|
17
13
|
|
18
14
|
def initialize(options={})
|
19
15
|
super("cache", options)
|
20
16
|
|
21
17
|
@logger = Logger.new(STDOUT)
|
18
|
+
@logger.level = Logger::INFO
|
22
19
|
|
23
20
|
@cache_name = options[:cache_name] || options['cache_name'] || "default"
|
24
21
|
|
25
|
-
load_from_hash(
|
22
|
+
load_from_hash('defaults', {
|
23
|
+
:scheme => 'https',
|
26
24
|
:host => AWS_US_EAST_HOST,
|
27
25
|
:port => 443,
|
28
26
|
:api_version => 1,
|
29
|
-
:user_agent => 'iron_cache_ruby-' + IronCache::VERSION + ' (iron_core_ruby-' + IronCore.version + ')')
|
27
|
+
:user_agent => 'iron_cache_ruby-' + IronCache::VERSION + ' (iron_core_ruby-' + IronCore.version + ')'})
|
30
28
|
|
31
29
|
if (not @token) || (not @project_id)
|
32
|
-
IronCore::Logger.error '
|
30
|
+
IronCore::Logger.error 'IronCache', 'Both token and project_id must be specified'
|
33
31
|
raise IronCore::IronError.new('Both token and project_id must be specified')
|
34
32
|
end
|
35
33
|
|
@@ -40,8 +38,8 @@ module IronCache
|
|
40
38
|
return Items.new(self)
|
41
39
|
end
|
42
40
|
|
43
|
-
def cache
|
44
|
-
|
41
|
+
def cache(name)
|
42
|
+
return Cache.new(self, {"name"=>name})
|
45
43
|
end
|
46
44
|
|
47
45
|
def caches
|
data/lib/iron_cache/items.rb
CHANGED
@@ -10,7 +10,7 @@ module IronCache
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def path(key, options={})
|
13
|
-
path = "projects/#{@client.project_id}/caches/#{URI.escape(options[:cache_name] || @client.cache_name)}/items/#{URI.escape key}"
|
13
|
+
path = "projects/#{@client.project_id}/caches/#{URI.escape(options[:cache_name] || @client.cache_name)}/items/#{URI.escape key}#{'/increment' if options[:increment] == true}"
|
14
14
|
end
|
15
15
|
|
16
16
|
# options:
|
@@ -23,7 +23,7 @@ module IronCache
|
|
23
23
|
json = @client.parse_response(res, true)
|
24
24
|
return Item.new(self, json)
|
25
25
|
rescue IronCore::IronResponseError => ex
|
26
|
-
|
26
|
+
@client.logger.debug ex.inspect
|
27
27
|
if ex.code == 404
|
28
28
|
return nil
|
29
29
|
end
|
@@ -43,6 +43,17 @@ module IronCache
|
|
43
43
|
#return Message.new(self, res)
|
44
44
|
return ResponseBase.new(json)
|
45
45
|
end
|
46
|
+
|
47
|
+
# options:
|
48
|
+
# :cache_name => can specify an alternative queue name
|
49
|
+
def increment(key, amount=1, options={})
|
50
|
+
options = options.merge(:increment => true)
|
51
|
+
res = @client.post(path(key, options), {:amount => amount})
|
52
|
+
hash = @client.parse_response(res, true)
|
53
|
+
@client.logger.debug "increment response: " + hash.inspect
|
54
|
+
hash["key"] ||= key
|
55
|
+
return Item.new(self, hash)
|
56
|
+
end
|
46
57
|
|
47
58
|
def delete(key, options={})
|
48
59
|
path2 = "#{self.path(key, options)}"
|
@@ -67,9 +78,9 @@ module IronCache
|
|
67
78
|
raw[key]
|
68
79
|
end
|
69
80
|
|
70
|
-
def id
|
71
|
-
|
72
|
-
end
|
81
|
+
#def id
|
82
|
+
# raw["id"]
|
83
|
+
#end
|
73
84
|
|
74
85
|
def msg
|
75
86
|
raw["msg"]
|
@@ -93,7 +104,7 @@ module IronCache
|
|
93
104
|
end
|
94
105
|
|
95
106
|
def delete
|
96
|
-
@messages.delete(self.
|
107
|
+
@messages.delete(self.key)
|
97
108
|
end
|
98
109
|
end
|
99
110
|
|
data/lib/iron_cache/version.rb
CHANGED
data/test/quick_run.rb
CHANGED
data/test/test_base.rb
CHANGED
data/test/test_iron_cache.rb
CHANGED
@@ -8,6 +8,17 @@ class IronCacheTests < TestBase
|
|
8
8
|
super
|
9
9
|
|
10
10
|
end
|
11
|
+
def test_performance_put_message
|
12
|
+
@client.cache_name = 'test_basics'
|
13
|
+
res = @client.items.put("key", "value")
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_performance_put_100_messages
|
17
|
+
@client.cache_name = 'test_basics'
|
18
|
+
100.times do
|
19
|
+
res = @client.items.put("key", "value")
|
20
|
+
end
|
21
|
+
end
|
11
22
|
|
12
23
|
def test_basics
|
13
24
|
@client.cache_name = 'test_basics'
|
@@ -35,6 +46,43 @@ class IronCacheTests < TestBase
|
|
35
46
|
p res
|
36
47
|
assert res.nil?
|
37
48
|
|
49
|
+
# new style of referencing cache
|
50
|
+
cache = @client.cache("test_basics")
|
51
|
+
res = cache.put(k,v)
|
52
|
+
p res
|
53
|
+
assert res.msg
|
54
|
+
|
55
|
+
res = cache.get(k)
|
56
|
+
p res
|
57
|
+
assert res["key"]
|
58
|
+
assert res.key
|
59
|
+
assert res.key == k
|
60
|
+
assert res.value == v
|
61
|
+
|
62
|
+
res = cache.delete(k)
|
63
|
+
p res
|
64
|
+
puts "shouldn't be any more"
|
65
|
+
res = cache.get(k)
|
66
|
+
p res
|
67
|
+
assert res.nil?
|
68
|
+
|
69
|
+
# test delete by item
|
70
|
+
res = cache.put(k,v)
|
71
|
+
p res
|
72
|
+
assert res.msg
|
73
|
+
|
74
|
+
res = cache.get(k)
|
75
|
+
p res
|
76
|
+
assert res.value
|
77
|
+
res = res.delete
|
78
|
+
p res
|
79
|
+
puts "shouldn't be any more"
|
80
|
+
res = cache.get(k)
|
81
|
+
p res
|
82
|
+
assert res.nil?
|
83
|
+
|
84
|
+
|
85
|
+
|
38
86
|
end
|
39
87
|
|
40
88
|
def test_caches
|
@@ -44,6 +92,17 @@ class IronCacheTests < TestBase
|
|
44
92
|
assert caches.is_a?(Array)
|
45
93
|
assert caches.size > 0
|
46
94
|
assert caches[0].name
|
95
|
+
#assert caches[0].size
|
96
|
+
|
97
|
+
cache = @client.caches.get(caches[0].name)
|
98
|
+
p cache
|
99
|
+
assert cache.name
|
100
|
+
#assert cache.size
|
101
|
+
|
102
|
+
cache = @client.cache(caches[0])
|
103
|
+
p cache
|
104
|
+
assert cache.name
|
105
|
+
#assert cache.size
|
47
106
|
|
48
107
|
end
|
49
108
|
|
@@ -64,5 +123,51 @@ class IronCacheTests < TestBase
|
|
64
123
|
|
65
124
|
end
|
66
125
|
|
126
|
+
def test_incrementors
|
127
|
+
@client.cache_name = 'test_incrementors'
|
128
|
+
clear_queue
|
129
|
+
k = "incr1"
|
130
|
+
v = 1
|
131
|
+
res = @client.items.put(k, v)
|
132
|
+
p res
|
133
|
+
|
134
|
+
res = @client.items.get(k)
|
135
|
+
p res
|
136
|
+
assert res.key == k
|
137
|
+
assert res.value == v, "value #{res.value.inspect} does not equal v: #{v.inspect}"
|
138
|
+
|
139
|
+
incr_by = 10
|
140
|
+
res = @client.items.increment(k, incr_by)
|
141
|
+
res = @client.items.get(k)
|
142
|
+
assert res.value == (v + incr_by)
|
143
|
+
|
144
|
+
res = @client.items.increment(k, -6)
|
145
|
+
assert res.value == 5
|
146
|
+
|
147
|
+
res.delete
|
148
|
+
|
149
|
+
# new style
|
150
|
+
cache = @client.cache("test_incrementors")
|
151
|
+
res = cache.put(k, v)
|
152
|
+
p res
|
153
|
+
|
154
|
+
res = cache.get(k)
|
155
|
+
p res
|
156
|
+
assert res.key == k
|
157
|
+
assert res.value == v, "value #{res.value.inspect} does not equal v: #{v.inspect}"
|
158
|
+
|
159
|
+
incr_by = 10
|
160
|
+
res = cache.increment(k, incr_by)
|
161
|
+
res = cache.get(k)
|
162
|
+
assert res.value == (v + incr_by)
|
163
|
+
|
164
|
+
res = cache.increment(k, -6)
|
165
|
+
assert res.value == 5
|
166
|
+
|
167
|
+
res.delete
|
168
|
+
|
169
|
+
|
170
|
+
end
|
171
|
+
|
67
172
|
end
|
68
173
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iron_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: iron_core
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 0.1.4
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 0.1.4
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: test-unit
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -91,6 +91,22 @@ dependencies:
|
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: typhoeus
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
94
110
|
description: Ruby client for IronCache by www.iron.io
|
95
111
|
email:
|
96
112
|
- treeder@gmail.com
|