cache_store 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cache_store.rb +44 -74
- data/lib/cache_store/version.rb +1 -1
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93720eeece64e0348cdd1429dd754b61e585b149
|
4
|
+
data.tar.gz: a8e5be29d169ab8f6c591c74b318f7792edcb2cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85e5d98a7b73fd8f70b376d70b37975b7394e98dc9eee1130021a3e09be8bdb85edf44022f519fe34da7253e931300159f29492b766e14c51b46e70a807d8d77
|
7
|
+
data.tar.gz: 45cb0f6d93159bea98aac9b1b9abbc0771b4778c78de191cd9697f4836ab75faff34836bdfb6ba0c1eaba1517216026e4b356b2271d9df24f5d02251f8a4be46
|
data/lib/cache_store.rb
CHANGED
@@ -1,130 +1,100 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cache_store/version'
|
2
4
|
require 'date'
|
3
5
|
require 'time'
|
4
|
-
#This class is used to define the contract that CacheStore implementations must adhere to.
|
5
|
-
class CacheStoreContract
|
6
6
|
|
7
|
+
# Defines the contract that CacheStore implementations must adhere to.
|
8
|
+
class CacheStoreContract
|
7
9
|
def initialize(namespace = '')
|
8
|
-
@namespace = namespace
|
9
10
|
end
|
10
11
|
|
11
|
-
#
|
12
|
+
# Sets a value within this cache store by its key.
|
12
13
|
#
|
13
14
|
# @param [String] This is the unique key to reference the value being set within this cache store.
|
14
15
|
# @param [Object] This is the value to set within this cache store.
|
15
16
|
# @param [Integer] This is the number of seconds from the current time that this value should expire.
|
16
17
|
def set(key, value, expires_in = 0)
|
17
|
-
|
18
18
|
end
|
19
19
|
|
20
|
-
#
|
20
|
+
# Gets a value from this cache store by its unique key.
|
21
21
|
#
|
22
22
|
# @param [String] This is the unique key to reference the value to fetch from within this cache store.
|
23
23
|
# @param [Integer] This is the number of seconds from the current time that this value should expire. (This is used in conjunction with the block to hydrate the cache key if it is empty.)
|
24
24
|
# @param [Block] This block is provided to hydrate this cache store with the value for the request key when it is not found.
|
25
25
|
def get(key, expires_in = 0, &block)
|
26
|
-
|
27
26
|
end
|
28
27
|
|
29
|
-
#
|
28
|
+
# Removes a value from this cache store by its unique key.
|
30
29
|
#
|
31
30
|
# @param [String] This is the unique key to reference the value to remove from this cache store.
|
32
31
|
def remove(key)
|
33
|
-
|
34
32
|
end
|
35
33
|
|
36
|
-
#
|
34
|
+
# Checks if a value exists within this cache store for a specific key.
|
37
35
|
#
|
38
36
|
# @param [String] This is the unique key to reference the value to check for within this cache store.
|
39
37
|
def exist?(key)
|
40
|
-
|
41
38
|
end
|
42
39
|
end
|
43
40
|
|
44
|
-
#
|
41
|
+
# Implements a local in-memory cache store.
|
45
42
|
class LocalCacheStore
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
def initialize(namespace = nil)
|
50
|
-
@store = Array.new
|
51
|
-
@namespace = namespace
|
43
|
+
def initialize(_namespace = nil)
|
44
|
+
@store = {}
|
52
45
|
end
|
53
46
|
|
54
|
-
#
|
47
|
+
# Stores a value in this cache store by its key.
|
55
48
|
#
|
56
|
-
# @param key [String]
|
57
|
-
# @param value [Object]
|
58
|
-
# @param expires_in [Integer]
|
49
|
+
# @param key [String] The unique key to reference the value being set.
|
50
|
+
# @param value [Object] The value to store.
|
51
|
+
# @param expires_in [Integer] The number of seconds from the current time that this value should expire.
|
59
52
|
def set(key, value, expires_in = 0)
|
60
|
-
remove(key)
|
61
|
-
expires = nil
|
62
53
|
if expires_in > 0
|
63
|
-
expires = Time.now
|
54
|
+
expires = Time.now + expires_in
|
64
55
|
end
|
65
|
-
@store.
|
56
|
+
@store.store(key, {value: value, expires: expires})
|
66
57
|
end
|
67
58
|
|
68
|
-
#
|
59
|
+
# Gets a value from this cache store by its unique key.
|
69
60
|
#
|
70
|
-
# @param key [String]
|
71
|
-
# @param
|
72
|
-
# @param &block [Block] This block is provided to hydrate this cache store with the value for the request key when it is not found.
|
61
|
+
# @param key [String] Unique key to reference the value to fetch from within this cache store.
|
62
|
+
# @param &block [Block] This block is provided to populate this cache store with the value for the request key when it is not found.
|
73
63
|
# @return [Object] The value for the specified unique key within the cache store.
|
74
|
-
def get(key, expires_in = 0
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
#put the item in the store
|
86
|
-
set(build_key(key), value, expires_in)
|
87
|
-
return value
|
88
|
-
else
|
89
|
-
#no hydration block was specified
|
90
|
-
|
91
|
-
#check if an expired item was found
|
92
|
-
if item != nil
|
93
|
-
#remove the expired item from the store
|
94
|
-
remove(build_key(key))
|
64
|
+
def get(key, expires_in = 0)
|
65
|
+
item = @store[key]
|
66
|
+
if item
|
67
|
+
if item[:expires] && item[:expires] < Time.now # An expired entry has been found
|
68
|
+
if block_given?
|
69
|
+
value = yield
|
70
|
+
set(key, value, expires_in)
|
71
|
+
return value
|
72
|
+
else
|
73
|
+
remove(key)
|
74
|
+
return nil
|
95
75
|
end
|
96
|
-
|
76
|
+
else # An item was found which has not expired
|
77
|
+
return item[:value]
|
97
78
|
end
|
79
|
+
elsif block_given?
|
80
|
+
value = yield
|
81
|
+
set(key, value, expires_in)
|
82
|
+
return value
|
98
83
|
end
|
99
|
-
|
100
|
-
#return the item
|
101
|
-
return item[:value]
|
102
84
|
end
|
103
85
|
|
104
|
-
#
|
86
|
+
# Removes a value from this cache store by its unique key.
|
105
87
|
#
|
106
|
-
# @param key [String]
|
88
|
+
# @param key [String] The unique key to remove from this cache store.
|
107
89
|
def remove(key)
|
108
|
-
@store.
|
90
|
+
@store.delete key
|
109
91
|
end
|
110
92
|
|
111
|
-
#
|
93
|
+
# Checks if a value exists within this cache store for a specific key.
|
112
94
|
#
|
113
|
-
# @param key [String]
|
95
|
+
# @param key [String] The unique key to reference the value to check for within this cache store.
|
114
96
|
# @return [Boolean] True or False to specify if the key exists in the cache store.
|
115
97
|
def exist?(key)
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
private
|
120
|
-
|
121
|
-
def build_key(key)
|
122
|
-
k = ''
|
123
|
-
if @namespace != nil
|
124
|
-
k = @namespace + ':' + key.to_s
|
125
|
-
elsif
|
126
|
-
k = key.to_s
|
127
|
-
end
|
128
|
-
k
|
98
|
+
@store.key? key
|
129
99
|
end
|
130
100
|
end
|
data/lib/cache_store/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vaughanbrittonsage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,4 +99,3 @@ summary: This is the base for a cache framework that includes a basic in memory
|
|
99
99
|
store, along with a dependency contract for additional provider implementations
|
100
100
|
plugins.
|
101
101
|
test_files: []
|
102
|
-
has_rdoc:
|