simple_cache_rs 0.11.1 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ class SimpleCache::MemcachedStore
2
+ Marshal = ::SimpleCache::Marshal
3
+
4
+ def initialize(url)
5
+ require "dalli"
6
+
7
+ uri = URI.parse(url)
8
+
9
+ @dc = Dalli::Client.new("#{uri.host}:#{uri.port}", :namespace => uri.path || "simple_cache", :compress => true)
10
+ end
11
+
12
+ def fetch(key, &block)
13
+ value = @dc.get(key)
14
+ return Marshal.unmarshal(value) if value
15
+ return yield(self, key) if block
16
+ nil
17
+ end
18
+
19
+ def store(key, value, ttl = nil)
20
+ @dc.set(key, Marshal.marshal(value), ttl)
21
+ value
22
+ end
23
+
24
+ def clear
25
+ @dc.flush_all
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ class SimpleCache::NullStore
2
+ def clear
3
+ end
4
+
5
+ def fetch(key, &block)
6
+ yield self, key if block_given?
7
+ end
8
+
9
+ def store(key, value, max_age = nil)
10
+ value
11
+ end
12
+ end
@@ -3,7 +3,7 @@ require "micro_sql"
3
3
  class SimpleCache::PgStore
4
4
  Marshal = ::SimpleCache::Marshal
5
5
 
6
- TABLE_NAME = "simple_cache"
6
+ TABLE_NAME = "simple_cache2"
7
7
 
8
8
  attr :path
9
9
 
@@ -18,18 +18,18 @@ class SimpleCache::PgStore
18
18
 
19
19
  def fetch(key, &block)
20
20
  value = table[key]
21
- return value if value
21
+ return Marshal.unmarshal(value) if value
22
22
  return yield(self, key) if block
23
23
  nil
24
24
  end
25
25
 
26
26
  def store(key, value, ttl = nil)
27
- table.update(key, value, ttl)
27
+ table.update(key, Marshal.marshal(value), ttl)
28
28
  value
29
29
  end
30
30
 
31
31
  def clear
32
- return unless @db.tables.include?(TABLE_NAME)
33
- @db.ask "DELETE FROM #{TABLE_NAME}"
32
+ return unless @db.tables.include?(table.table_name)
33
+ @db.ask "DELETE FROM #{table.table_name}"
34
34
  end
35
35
  end
@@ -1,9 +1,10 @@
1
1
  # A simplistic Sqlite database interface
2
2
  class SimpleCache::SqliteDatabase
3
- def initialize(path)
3
+ def initialize(url)
4
4
  require "sqlite3"
5
5
 
6
- FileUtils.mkdir_p File.dirname(@path)
6
+ path = URI.parse(url).path
7
+ FileUtils.mkdir_p File.dirname(path)
7
8
 
8
9
  @impl = SQLite3::Database.new(path)
9
10
  @prepared_queries = {}
@@ -27,9 +28,9 @@ class SimpleCache::SqliteStore < SimpleCache::SqliteDatabase
27
28
 
28
29
  def self.base_dir
29
30
  if RUBY_PLATFORM.downcase.include?("darwin")
30
- "#{Dir.home}/Library/Cache"
31
+ "#{Dir.home}/Library/Caches/org.radiospiel.simple_cache"
31
32
  else
32
- "#{Dir.home}/cache"
33
+ "#{Dir.home}/.org.radiospiel.simple_cache"
33
34
  end
34
35
  end
35
36
 
data/lib/simple_cache.rb CHANGED
@@ -6,30 +6,32 @@ end
6
6
 
7
7
  require_relative "simple_cache/marshal"
8
8
 
9
+ require_relative "simple_cache/memcached_store"
10
+ require_relative "simple_cache/pg_store"
9
11
  require_relative "simple_cache/sqlite_store"
10
12
  require_relative "simple_cache/redis_store"
13
+ require_relative "simple_cache/null_store"
11
14
 
12
15
  module SimpleCache
13
16
  def self.create_store(url)
14
- expect! url => [ String, Redis, Redis::Namespace ]
15
- case url
16
- when Redis, Redis::Namespace then
17
- return SimpleCache::RedisStore.new(url)
18
- when String
17
+ expect! url => [ "null:", nil, String, Redis, Redis::Namespace ]
18
+
19
+ klass = case url
20
+ when Redis, Redis::Namespace then SimpleCache::RedisStore
21
+ when "null:", nil then SimpleCache::NullStore
22
+ else
19
23
  uri = URI.parse(url)
24
+ expect! uri.scheme => [ nil, "redis", "sqlite", "pg", "memcached" ]
20
25
 
21
26
  case uri.scheme
22
- when "redis" then
23
- return SimpleCache::RedisStore.new(url)
24
- when nil, "sqlite" then
25
- return SimpleCache::SqliteStore.new(uri.path)
26
- when "pg"
27
- require_relative "simple_cache/pg_store"
28
- return SimpleCache::PgStore.new(url)
29
- else
30
- raise uri.scheme.inspect
27
+ when "redis" then SimpleCache::RedisStore
28
+ when nil, "sqlite" then SimpleCache::SqliteStore
29
+ when "pg" then SimpleCache::PgStore
30
+ when "memcached" then SimpleCache::MemcachedStore
31
31
  end
32
32
  end
33
+
34
+ klass.new url
33
35
  end
34
36
 
35
37
  def self.new(url)
@@ -0,0 +1,31 @@
1
+ require_relative 'test_helper'
2
+
3
+ class SimpleCache::MemcachedStoreTest < Test::Unit::TestCase
4
+ include SimpleCache::TestCase
5
+
6
+ def simple_cache
7
+ @simple_cache ||= begin
8
+ simple_cache = SimpleCache.new("memcached://localhost:11211/simple_cache_test")
9
+ simple_cache.clear
10
+ simple_cache
11
+ end
12
+ end
13
+
14
+ def test_expiration
15
+ assert_equal("1", simple_cache.store("forever", "1"))
16
+ assert_equal("2", simple_cache.store("young", "2", 3))
17
+
18
+ # Timecop tests don't work here :(
19
+ #
20
+ # Timecop.freeze(Time.now + 10) do
21
+ # assert_equal("1", simple_cache.fetch("forever"))
22
+ # assert_equal(nil, simple_cache.fetch("young"))
23
+ # end
24
+ end
25
+
26
+ def test_array
27
+ ary = %w(foo bar)
28
+ assert_equal(ary, simple_cache.store("ary", ary))
29
+ assert_equal(ary, simple_cache.fetch("ary"))
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'test_helper'
2
+
3
+ class SimpleCache::NullStoreTest < Test::Unit::TestCase
4
+ include SimpleCache::TestCase
5
+
6
+ URL = "null:"
7
+
8
+ def simple_cache
9
+ @simple_cache ||= SimpleCache.new(URL).tap(&:clear)
10
+ end
11
+
12
+ def teardown
13
+ @simple_cache.clear if @simple_cache
14
+ end
15
+ end
@@ -4,7 +4,11 @@ class SimpleCache::PgStoreTest < Test::Unit::TestCase
4
4
  include SimpleCache::TestCase
5
5
 
6
6
  def simple_cache
7
- @simple_cache ||= SimpleCache.new("pg://simple_cache:simple_cache@localhost/simple_cache_test").tap(&:clear)
7
+ @simple_cache ||= begin
8
+ simple_cache = SimpleCache.new("pg://simple_cache:simple_cache@localhost:5432/simple_cache_test")
9
+ simple_cache.clear
10
+ simple_cache
11
+ end
8
12
  end
9
13
 
10
14
  def test_expiration
@@ -15,4 +19,10 @@ class SimpleCache::PgStoreTest < Test::Unit::TestCase
15
19
  assert_equal(nil, simple_cache.fetch("young"))
16
20
  end
17
21
  end
22
+
23
+ def test_array
24
+ ary = %w(foo bar)
25
+ assert_equal(ary, simple_cache.store("ary", ary))
26
+ assert_equal(ary, simple_cache.fetch("ary"))
27
+ end
18
28
  end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
3
  begin
4
- Bundler.setup(:default, :development)
4
+ Bundler.setup(:default, :development, :test)
5
5
  rescue Bundler::BundlerError => e
6
6
  $stderr.puts e.message
7
7
  $stderr.puts "Run `bundle install` to install missing gems"
@@ -27,7 +27,12 @@ module SimpleCache::TestCase
27
27
  assert_equal(nil, simple_cache.fetch("bar"))
28
28
 
29
29
  assert_equal("foo", simple_cache.store("bar", "foo"))
30
- assert_equal("foo", simple_cache.fetch("bar"))
30
+
31
+ if simple_cache.is_a?(SimpleCache::NullStore)
32
+ assert_equal(nil, simple_cache.fetch("bar"))
33
+ else
34
+ assert_equal("foo", simple_cache.fetch("bar"))
35
+ end
31
36
  end
32
37
 
33
38
  def test_simple_cache_expiration
@@ -35,7 +40,21 @@ module SimpleCache::TestCase
35
40
  assert_equal "baz", simple_cache.cached("key2") { done += 1; "baz" }
36
41
  assert_equal 1, done
37
42
  assert_equal "baz", simple_cache.cached("key2") { done += 1; "baz" }
43
+
44
+ if simple_cache.is_a?(SimpleCache::NullStore)
45
+ assert_equal 2, done
46
+ else
47
+ assert_equal 1, done
48
+ end
49
+ end
50
+
51
+ def test_simple_cache_clear
52
+ done = 0
53
+ assert_equal "baz", simple_cache.cached("test_simple_cache_expiration") { done += 1; "baz" }
38
54
  assert_equal 1, done
55
+ simple_cache.clear
56
+ assert_equal "baz", simple_cache.cached("test_simple_cache_expiration") { done += 1; "baz" }
57
+ assert_equal 2, done
39
58
  end
40
59
 
41
60
  def test_simple_cache_store_and_fetch
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_cache_rs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.12.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-01 00:00:00.000000000 Z
12
+ date: 2013-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: expectation
16
- requirement: &70272344276440 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70272344276440
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: redis
27
- requirement: &70272344248420 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70272344248420
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: redis-namespace
38
- requirement: &70272344243080 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70272344243080
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: micro_sql
49
- requirement: &70272344242040 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :runtime
56
71
  prerelease: false
57
- version_requirements: *70272344242040
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: A sensibly fast, yet simplistic sqlite- or redis-based cache.
59
79
  email: eno@radiospiel.org
60
80
  executables: []
@@ -62,6 +82,8 @@ extensions: []
62
82
  extra_rdoc_files: []
63
83
  files:
64
84
  - lib/simple_cache/marshal.rb
85
+ - lib/simple_cache/memcached_store.rb
86
+ - lib/simple_cache/null_store.rb
65
87
  - lib/simple_cache/pg_store.rb
66
88
  - lib/simple_cache/redis_store.rb
67
89
  - lib/simple_cache/sqlite_store.rb
@@ -117,7 +139,9 @@ files:
117
139
  - test/coverage/assets/0.5.3/smoothness/jquery-ui-1.8.4.custom.css
118
140
  - test/coverage/assets/0.5.3/stylesheet.css
119
141
  - test/coverage/index.html
142
+ - test/memcache_store_test.rb
120
143
  - test/memoize_test.rb
144
+ - test/null_store_test.rb
121
145
  - test/pg_store_test.rb
122
146
  - test/redis_store_test.rb
123
147
  - test/simple_cache_test.rb
@@ -137,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
161
  version: '0'
138
162
  segments:
139
163
  - 0
140
- hash: 3223836389259576757
164
+ hash: -4118416796349753026
141
165
  required_rubygems_version: !ruby/object:Gem::Requirement
142
166
  none: false
143
167
  requirements:
@@ -146,10 +170,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
170
  version: '0'
147
171
  segments:
148
172
  - 0
149
- hash: 3223836389259576757
173
+ hash: -4118416796349753026
150
174
  requirements: []
151
175
  rubyforge_project:
152
- rubygems_version: 1.8.10
176
+ rubygems_version: 1.8.25
153
177
  signing_key:
154
178
  specification_version: 3
155
179
  summary: A sensibly fast, yet simplistic sqlite- or redis-based cache.