h2ocube_rails_cache 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d9a6b8ef97b0726b986a78456b0acc7f58ba457
4
- data.tar.gz: d4dc1c0bee67215dc08bcaebc4468ccb0d2c1c2b
3
+ metadata.gz: e7360a8fa112914930362a58fef207c6a903b0a8
4
+ data.tar.gz: 9b36bbe3952252515b46adaf292e8ea8669c0cc1
5
5
  SHA512:
6
- metadata.gz: b3004ea8f51b991842e8a4995151f4af34f02736ef164e69a75ddc75e98b6ea0a9925d4eba83dcbf8aec5945c3218721f08176be8f894b184a0b7d343e21f364
7
- data.tar.gz: c9db60273fdac7ee363b69cda7abaf3bc75a6cb52cad8947d8395015864575c9a3d8aca9f8b074a63f76cdc8cac5ef311b5680c5e90df62a7fa83743a44bf241
6
+ metadata.gz: 885c6717c9aa1f55d43f3e9cf01d8a1bda4dc751cadb5077ae59f0bd10f74004a9bc89477f46ff7853b35b9aaaf8e1a42bd8725315e2703b9470e9382f309efd
7
+ data.tar.gz: 0c361281730b2bf7872f8be2a91b1a1141c6d33c0d4ff7dff6e97a7d2060cddf8d35e6faae96fcd96e2a2f072957208c81aeb613832093cd4eb7e6566c66eba4
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = 'h2ocube_rails_cache'
7
- gem.version = '0.0.4'
7
+ gem.version = '0.0.5'
8
8
  gem.authors = ['Ben']
9
9
  gem.email = ['ben@h2ocube.com']
10
10
  gem.description = 'Just an redis cache.'
@@ -14,27 +14,34 @@ module ActiveSupport
14
14
  @data.keys key
15
15
  end
16
16
 
17
- def read(key, options = nil)
17
+ def read key, options = nil
18
+ key = expanded_key key
18
19
  return nil if key.start_with?('http')
19
20
  if exist? key
20
- Marshal.load(@data.get key)
21
+ load_entry(@data.get key)
21
22
  else
22
23
  nil
23
24
  end
24
25
  end
25
26
 
26
- def write(key, entry, options = nil)
27
+ def read_raw key, options = nil
28
+ @data.get key
29
+ end
30
+
31
+
32
+ def write key, entry, options = nil
33
+ key = expanded_key key
27
34
  return false if key.start_with?('http')
28
- @data.set key, Marshal.dump(entry), options
35
+ @data.set key, dump_entry(entry), options
29
36
  true
30
37
  end
31
38
 
32
- def delete(name, options = nil)
39
+ def delete name, options = nil
33
40
  @data.keys(name).each{ |k| @data.del k }
34
41
  true
35
42
  end
36
43
 
37
- def exist?(name, options = nil)
44
+ def exist? name, options = nil
38
45
  @data.exists name
39
46
  end
40
47
 
@@ -42,6 +49,52 @@ module ActiveSupport
42
49
  @data.keys('*').each{ |k| @data.del k }
43
50
  true
44
51
  end
52
+
53
+ def info
54
+ @data.info
55
+ end
56
+
57
+ def increment key, amount = 1, options = nil
58
+ if amount == 1
59
+ @data.incr key
60
+ else
61
+ @data.incrby key, amount
62
+ end
63
+ end
64
+
65
+ def decrement key, amount = 1, options = nil
66
+ if amount == 1
67
+ @data.decr key
68
+ else
69
+ @data.decrby key, amount
70
+ end
71
+ end
72
+
73
+ alias_method :read_entry, :read
74
+ alias_method :write_entry, :write
75
+ alias_method :delete_entry, :delete
76
+
77
+ private
78
+
79
+ def dump_entry entry
80
+ case entry.class.to_s
81
+ when 'String', 'Fixnum', 'Float'
82
+ entry
83
+ else
84
+ Marshal.dump entry
85
+ end
86
+ end
87
+
88
+ def load_entry entry
89
+ begin
90
+ Marshal.load entry
91
+ rescue
92
+ return entry.to_f if entry.respond_to?(:to_f) && entry.to_f.to_s == entry
93
+ return entry.to_i if entry.respond_to?(:to_i) && entry.to_i.to_s == entry
94
+
95
+ entry
96
+ end
97
+ end
45
98
  end
46
99
  end
47
100
  end
data/test/cache_test.rb CHANGED
@@ -5,6 +5,7 @@ describe 'h2ocube_rails_cache' do
5
5
  @redis = Redis.new
6
6
  @cache_key = Rails.application.class.to_s.split("::").first << ':Cache'
7
7
  @cache = Redis::Namespace.new(@cache_key)
8
+ Rails.cache.clear
8
9
  end
9
10
 
10
11
  it 'should work' do
@@ -24,13 +25,13 @@ describe 'h2ocube_rails_cache' do
24
25
  end
25
26
 
26
27
  it '.write, .exist?, .read and .delete' do
27
- Rails.cache.write('a', 'true').must_be_same_as true
28
+ Rails.cache.write('a', true).must_be_same_as true
28
29
 
29
30
  Rails.cache.exist?('a').must_be_same_as true
30
31
 
31
- Rails.cache.read('a').must_equal 'true'
32
+ Rails.cache.read('a').must_equal true
32
33
 
33
- Marshal.load(@redis.get("#{@cache_key}:a")).must_equal 'true'
34
+ Marshal.load(@redis.get("#{@cache_key}:a")).must_equal true
34
35
 
35
36
  Rails.cache.delete('a').must_be_same_as true
36
37
 
@@ -46,6 +47,42 @@ describe 'h2ocube_rails_cache' do
46
47
  sleep 1
47
48
  Rails.cache.exist?('expire').must_be_same_as false
48
49
  end
50
+
51
+ it 'key class' do
52
+ Rails.cache.write ['a', 0], 'a0'
53
+ Rails.cache.keys[0].must_equal 'a/0'
54
+ Rails.cache.clear
55
+
56
+ Rails.cache.write({a: 0}, 'a0')
57
+ Rails.cache.keys[0].must_equal 'a=0'
58
+ Rails.cache.clear
59
+ end
60
+
61
+ it 'value class' do
62
+ Rails.cache.write 'String', 'String'
63
+ Rails.cache.read_raw('String').must_be_kind_of String
64
+
65
+ Rails.cache.write 'Fixnum', 1
66
+ Rails.cache.read('Fixnum').must_be_kind_of Fixnum
67
+
68
+ Rails.cache.write 'Float', 1.1
69
+ Rails.cache.read('Float').must_be_kind_of Float
70
+ end
71
+
72
+ it 'increment' do
73
+ Rails.cache.write 'number', 1
74
+ Rails.cache.increment 'number'
75
+ Rails.cache.read('number').must_equal 2
76
+
77
+ Rails.cache.decrement 'number'
78
+ Rails.cache.read('number').must_equal 1
79
+
80
+ Rails.cache.increment 'number', 2
81
+ Rails.cache.read('number').must_equal 3
82
+
83
+ Rails.cache.decrement 'number', 2
84
+ Rails.cache.read('number').must_equal 1
85
+ end
49
86
  end
50
87
 
51
88
  describe ApplicationController do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: h2ocube_rails_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-22 00:00:00.000000000 Z
11
+ date: 2013-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-namespace