binary42-fastcache 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/fastcache.rb CHANGED
@@ -21,6 +21,7 @@ require 'fastcache/memcache'
21
21
 
22
22
  # Interfaces
23
23
  require 'fastcache/interface/connection'
24
+ require 'fastcache/interface/marshaled'
24
25
  require 'fastcache/interface/dictionary'
25
26
  require 'fastcache/interface/evaluator'
26
27
 
@@ -1,3 +1,17 @@
1
- class FastCache::Evaluator < FastCache::Connection
1
+ module FastCache::Evaluator
2
2
 
3
- end
3
+ attr_accessor :evaluation_cache
4
+ attr_accessor :default_expiration
5
+
6
+ def cache_eval(key = "", expiration = @default_expiration)
7
+ maybe = @evaluation_cache.get(key)
8
+ if maybe.anything?
9
+ Marshal.load(maybe.value)
10
+ else
11
+ value = yield
12
+ @evaluation_cache.set(key, Marshal.dump(value), expiration || 0)
13
+ value
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,37 @@
1
+ class FastCache::MarshaledConnection < FastCache::Connection
2
+
3
+ alias raw_get get
4
+ alias raw_set set
5
+ alias raw_add add
6
+ alias raw_replace replace
7
+
8
+ module MarshaledProtocol
9
+
10
+ def get(*)
11
+ maybe = super
12
+ maybe.anything? ?
13
+ Marshal.load(maybe.value) :
14
+ nil
15
+ end
16
+
17
+ def set(_, value, *)
18
+ value = Marshal.dump(value)
19
+ super
20
+ self
21
+ end
22
+
23
+ def add(_, value, *)
24
+ value = Marshal.dump(value)
25
+ super.anything?
26
+ end
27
+
28
+ def replace(_, value, *)
29
+ value = Marshal.dump(value)
30
+ super.anything?
31
+ end
32
+
33
+ end
34
+
35
+ include MarshaledProtocol
36
+
37
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binary42-fastcache
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Mitchell
@@ -33,6 +33,7 @@ files:
33
33
  - lib/fastcache/bucket/modulus.rb
34
34
  - lib/fastcache/bucket/consistent.rb
35
35
  - lib/fastcache/interface/dictionary.rb
36
+ - lib/fastcache/interface/marshaled.rb
36
37
  - lib/fastcache/interface/evaluator.rb
37
38
  - lib/fastcache/interface/connection.rb
38
39
  - lib/fastcache/memcache.rb