hashcache 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0556a1ac315d90aaafa7350ba7c9c9919729203c
4
- data.tar.gz: 98b97f53caf559b041a4b834480da47e0db0ff24
3
+ metadata.gz: 0ef610ae53b9f8b47d9aed3e654d0e17ad172693
4
+ data.tar.gz: 7dcfa3898412a7f04e74423beb37fab5eef44aff
5
5
  SHA512:
6
- metadata.gz: 1952196f716d6070ea03786944bf34e2fad4ca1597d08798bf43092a71ae1ba9f8e63f2d9b2dfdf42c0bc14902eee8f5a82e69ca19c9d8e4c06dbb4e5081f3ad
7
- data.tar.gz: bd3b7de3b1b6fb2d1a6e21bde501a43f6b39427b178a5a988a0012cfa90973229afb39afedee50e06f2269232052346a0b1be17cc1b69982b475c855952a4243
6
+ metadata.gz: 2e20a58a3a329e3dd1682f9bb2aed9fe4fd0edfd5656e870738a22e1f28a3ba2e8712b5aa585d0d63e0daac40027f1c07d66a54f00128070c902282ebfd0865f
7
+ data.tar.gz: fc84505bdd4dadd51e78a10c5180e932465f130f92197c7eda02e15a1d04b28743e7727008048c43b04bd41147005a97ec4050ac6a6f1ce6967ac78ad0f22147
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/hashcache.rb CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  # file: hashcache.rb
4
4
 
5
+ require 'chronic_duration'
6
+
7
+
5
8
  class HashCache
6
9
  attr_accessor :size, :file_cache, :file_path
7
10
 
@@ -11,23 +14,36 @@ class HashCache
11
14
  @file_cache = o[:file_cache]
12
15
  @file_path = o[:file_path]
13
16
  @h = {}
17
+
18
+ def @h.expired?(item)
19
+ expiry_time = self.fetch(item)[:expiry_time]
20
+ return false unless expiry_time
21
+ expiry_time < Time.now
22
+ end
23
+
14
24
  end
15
25
 
16
26
  def read(item, &blk)
27
+
17
28
  if @h.include? item then
18
- self.refresh item
29
+ !@h.expired?(item) ? self.refresh(item) : @h.delete(item)[:val]
19
30
  else
20
31
  if @file_cache == true and File.exists? @file_path + '/' + item then
21
- read_file item
22
- else
23
- self.write(item) { blk.call }
32
+ read_file item
33
+ elsif block_given?
34
+ self.write(item) { blk.call }
24
35
  end
25
36
  end
26
37
  end
27
38
 
28
- def write(item, &blk)
29
- val = blk.call
30
- @h[item] = val
39
+ def write(item, duration=nil, &blk)
40
+
41
+ expiry_time = nil
42
+ val = blk.call
43
+
44
+ expiry_time = Time.now + ChronicDuration.parse(duration) if duration
45
+
46
+ @h[item] = {val: val, expiry: duration, expiry_time: expiry_time}
31
47
  @h.shift if @h.length > @size
32
48
  write_file(item, val) if @file_cache == true
33
49
  val
@@ -36,10 +52,10 @@ class HashCache
36
52
  alias read! write
37
53
 
38
54
  def refresh(item)
39
- val = @h[item]
55
+ x = @h[item]
40
56
  @h.delete item
41
- @h[item] = val
42
- val
57
+ @h[item] = x
58
+ x[:val]
43
59
  end
44
60
 
45
61
  def reset
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.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -29,7 +29,7 @@ cert_chain:
29
29
  4WRZoiYQodl3YerqYrzebXd975yLfnxaW9zyzb8IZuAh+wMKMmP8jx8+iYrKBMdL
30
30
  32sy9WjqlU2iPHUEVZWTEcgbtjC3IziV
31
31
  -----END CERTIFICATE-----
32
- date: 2013-07-27 00:00:00.000000000 Z
32
+ date: 2013-08-15 00:00:00.000000000 Z
33
33
  dependencies: []
34
34
  description:
35
35
  email: james@r0bertson.co.uk
metadata.gz.sig CHANGED
Binary file