hashcache 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hashcache.rb +23 -6
  2. metadata +2 -2
data/lib/hashcache.rb CHANGED
@@ -3,11 +3,13 @@
3
3
  # file: hashcache.rb
4
4
 
5
5
  class HashCache
6
- attr_accessor :size
6
+ attr_accessor :size, :file_cache, :file_path
7
7
 
8
8
  def initialize(opt={})
9
- o = {size: 5}.merge(opt)
9
+ o = {size: 5, file_cache: false, file_path: '/tmp'}.merge(opt)
10
10
  @size = o[:size]
11
+ @file_cache = o[:file_cache]
12
+ @file_path = o[:file_path]
11
13
  @h = {}
12
14
  end
13
15
 
@@ -15,10 +17,15 @@ class HashCache
15
17
  if @h.include? item then
16
18
  self.refresh item
17
19
  else
18
- val = yield
19
- @h[item] = val
20
- @h.shift if @h.length > @size
21
- val
20
+ if @file_cache == true and File.exists? @file_path + '/' + item then
21
+ read_file item
22
+ else
23
+ val = yield
24
+ @h[item] = val
25
+ @h.shift if @h.length > @size
26
+ write_file(item, val)
27
+ val
28
+ end
22
29
  end
23
30
  end
24
31
 
@@ -32,5 +39,15 @@ class HashCache
32
39
  def reset
33
40
  @h = {}
34
41
  end
42
+
43
+ private
44
+
45
+ def read_file(item)
46
+ File.open(item) {|f| doc = Marshal.load(f) }
47
+ end
48
+
49
+ def write_file(item, obj)
50
+ File.open(item, 'w+') {|f| Marshal.dump(obj, f) }
51
+ end
35
52
 
36
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashcache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-07-19 00:00:00 +01:00
12
+ date: 2010-07-24 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15