open-uri-cached 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.

Potentially problematic release.


This version of open-uri-cached might be problematic. Click here for more details.

Files changed (2) hide show
  1. data/lib/open-uri/cached.rb +20 -3
  2. metadata +3 -3
@@ -1,5 +1,6 @@
1
1
  require 'open-uri'
2
2
  require 'digest/sha1'
3
+ require 'yaml'
3
4
 
4
5
  module OpenURI
5
6
  class << self
@@ -21,9 +22,11 @@ module OpenURI
21
22
  end
22
23
 
23
24
  class Cache
24
- @cache_path = '/tmp/open-uri'
25
+ @cache_path = "/tmp/open-uri-#{Process.uid}"
25
26
 
26
27
  class << self
28
+ attr_accessor :cache_path
29
+
27
30
  ##
28
31
  # Retrieve file content and meta data from cache
29
32
  # @param [String] key
@@ -40,7 +43,7 @@ module OpenURI
40
43
  # Add meta accessors
41
44
  if meta && f
42
45
  f.instance_variable_set(:"@meta", meta)
43
-
46
+
44
47
  def f.meta
45
48
  @meta
46
49
  end
@@ -63,7 +66,7 @@ module OpenURI
63
66
  @meta[:status]
64
67
  end
65
68
  end
66
-
69
+
67
70
  f
68
71
  end
69
72
 
@@ -94,6 +97,20 @@ module OpenURI
94
97
  value
95
98
  end
96
99
 
100
+ # Invalidate cache for a key, optionally if older than time givan
101
+ # @param [String] key
102
+ # URL of content to be invalidated
103
+ # @param [Time] time
104
+ # (optional): the maximum age at which the cached value is still acceptable
105
+ # @return
106
+ # Returns 1 if a cached value was invalidated, false otherwise
107
+ def invalidate(key, time = Time.now)
108
+ filename = filename_from_url(key)
109
+ File.delete(filename) if File.stat(filename).mtime < time
110
+ rescue Errno::ENOENT
111
+ false
112
+ end
113
+
97
114
  protected
98
115
  def filename_from_url(url)
99
116
  uri = URI.parse(url) # TODO: rescue here?
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Danial Pearce
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-19 00:00:00 +10:00
17
+ date: 2012-01-08 00:00:00 +11:00
18
18
  default_executable:
19
19
  dependencies: []
20
20