jsoncache 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsoncache.rb +53 -32
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8b7e2aa8007592f2c122d828b77bf832c495d93
|
4
|
+
data.tar.gz: e7edbb2504cb92aac6ac04dcf12802c71487f152
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34ed662640fb70a27ee10b8cd42a2e938cb5a5626e1f4baf97fa9f637aab3b925645850da4a6ee7deb653f9cd9b60a14684644c65ba595c5ae77d67d9e79187d
|
7
|
+
data.tar.gz: 907ea8767eb4b034b087c9b8de095614c9c35167e103b21ca5c1a49c869d2752e41b62f5e3b4c33ef28c80d775ba2fef70ddf6b510e4e8da5114c9ca939bf9df
|
data/lib/jsoncache.rb
CHANGED
@@ -2,35 +2,35 @@ require 'json'
|
|
2
2
|
|
3
3
|
# JSONCache is a simple interface to cache JSON based API calls
|
4
4
|
module JSONCache
|
5
|
-
|
6
|
-
|
7
|
-
private
|
5
|
+
extend self
|
8
6
|
|
9
7
|
# Retrieves cached data for the specified key and caches the data provided
|
10
8
|
# if the cache isn't valid.
|
11
|
-
# @param [String] key
|
12
|
-
# @param [
|
13
|
-
#
|
14
|
-
|
15
|
-
|
9
|
+
# @param [String] key The key in which to check for cached data.
|
10
|
+
# @param [Hash] options A hash of the parameters to use when caching.
|
11
|
+
# Accepted options parameters
|
12
|
+
# => [Boolean] symbolize
|
13
|
+
# => [String] cache_directory
|
14
|
+
# => [Fixnum] delta
|
15
|
+
def cache(key, options = {})
|
16
|
+
return retrieve_cache(key, options) if cached?(key, options)
|
16
17
|
result = yield
|
17
|
-
cache_file(key, result)
|
18
|
+
cache_file(key, result, options)
|
18
19
|
result
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
22
|
+
private
|
23
|
+
|
24
|
+
########################################################################
|
25
|
+
# Core Caching
|
26
|
+
########################################################################
|
27
27
|
|
28
28
|
# Determine whether a file is cached and healthy
|
29
29
|
# @param [String] key The key in which to check for cached data.
|
30
|
-
# @param [
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
# @param [Hash] options A hash of the parameters to use when caching.
|
31
|
+
def cached?(key, options = {})
|
32
|
+
timestamp = timestamp_from_key(key, options[:cache_directory])
|
33
|
+
delta = options[:delta] || 0
|
34
34
|
if timestamp.zero?
|
35
35
|
false
|
36
36
|
elsif delta.zero?
|
@@ -43,11 +43,14 @@ module JSONCache
|
|
43
43
|
# Cache the result from the uri
|
44
44
|
# @param [String] key The key in which to check for cached data.
|
45
45
|
# @param [Hash] data The response to cache.
|
46
|
-
|
47
|
-
|
48
|
-
existing_file = filename_from_key(key)
|
49
|
-
last_path = "#{cache_path}/#{existing_file}"
|
46
|
+
# @param [Hash] options A hash of the parameters to use when caching.
|
47
|
+
def cache_file(key, data, options = {})
|
50
48
|
return unless data.respond_to?(:to_h)
|
49
|
+
|
50
|
+
cache_path = cache_dir(options[:cache_directory])
|
51
|
+
existing_file = filename_from_key(key, options[:cache_directory])
|
52
|
+
last_path = "#{cache_path}/#{existing_file}"
|
53
|
+
|
51
54
|
File.delete(last_path) if existing_file && File.exist?(last_path)
|
52
55
|
File.write(
|
53
56
|
"#{cache_path}/#{key}#{Time.now.to_i}.json",
|
@@ -56,19 +59,36 @@ module JSONCache
|
|
56
59
|
|
57
60
|
# Retrieves a cached value from a key
|
58
61
|
# @param [String] key The key in which to check for cached data.
|
59
|
-
|
60
|
-
|
62
|
+
# @param [Hash] options A hash of the parameters to use when caching.
|
63
|
+
def retrieve_cache(key, options = {})
|
64
|
+
directory = options[:cache_directory]
|
65
|
+
filename = filename_from_key(key, directory)
|
61
66
|
return nil if filename.nil?
|
62
|
-
|
67
|
+
symbolize = options[:symbolize] || false
|
63
68
|
JSON.parse(
|
64
|
-
File.read("#{cache_dir}/#{filename}"),
|
65
|
-
symbolize_names:
|
69
|
+
File.read("#{cache_dir(directory)}/#{filename}"),
|
70
|
+
symbolize_names: symbolize)
|
71
|
+
end
|
72
|
+
|
73
|
+
########################################################################
|
74
|
+
# Helpers
|
75
|
+
########################################################################
|
76
|
+
|
77
|
+
# Create, if necessary, and return a cache directory
|
78
|
+
# @param [String] directory The name of the cache directory.
|
79
|
+
def cache_dir(directory)
|
80
|
+
directory ||= 'jsoncache'
|
81
|
+
cache_path = File.join('/tmp', directory)
|
82
|
+
Dir.mkdir(cache_path) unless Dir.exist?(cache_path)
|
83
|
+
cache_path
|
66
84
|
end
|
67
85
|
|
68
86
|
# Gets an existing file from a uri if it exists
|
69
87
|
# @param [String] key The key in which to check for cached data.
|
70
|
-
|
71
|
-
|
88
|
+
# @param [String] directory The name of the cache directory.
|
89
|
+
def filename_from_key(key, directory)
|
90
|
+
directory ||= 'jsoncache'
|
91
|
+
Dir.foreach(cache_dir(directory)) do |filename|
|
72
92
|
next unless filename.include?(key)
|
73
93
|
return filename
|
74
94
|
end
|
@@ -76,8 +96,9 @@ module JSONCache
|
|
76
96
|
|
77
97
|
# Extracts a timestamp from an existing file
|
78
98
|
# @param [String] key The key in which to check for cached data.
|
79
|
-
|
80
|
-
|
99
|
+
# @param [String] directory The name of the cache directory.
|
100
|
+
def timestamp_from_key(key, directory)
|
101
|
+
path = filename_from_key(key, directory)
|
81
102
|
return 0 if path.nil?
|
82
103
|
path.slice(/#{key}.*/)
|
83
104
|
.gsub(/^#{key}/, '')
|