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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60903e1a173dbf748fc9ab48999c40efbe1853e1
4
- data.tar.gz: 868aab69ff4a3756ab914af8235ed5402dd99e57
3
+ metadata.gz: 93720eeece64e0348cdd1429dd754b61e585b149
4
+ data.tar.gz: a8e5be29d169ab8f6c591c74b318f7792edcb2cc
5
5
  SHA512:
6
- metadata.gz: 280b41390880517b20acf39e6430c6cb1d55aa0425d23fe9978ab20e6593c076f47da3155e3f297a770a11d368c8aa65a32c8895f5acdc75c47abfe7671f9f9e
7
- data.tar.gz: 506de091b4e66fb91ca7e16356029d68831890307061010471c4ad833295d72f26c3f2360b764bb7cdf11680717c6b0b640c1e112dbafd00e8f990a5a58425f4
6
+ metadata.gz: 85e5d98a7b73fd8f70b376d70b37975b7394e98dc9eee1130021a3e09be8bdb85edf44022f519fe34da7253e931300159f29492b766e14c51b46e70a807d8d77
7
+ data.tar.gz: 45cb0f6d93159bea98aac9b1b9abbc0771b4778c78de191cd9697f4836ab75faff34836bdfb6ba0c1eaba1517216026e4b356b2271d9df24f5d02251f8a4be46
data/lib/cache_store.rb CHANGED
@@ -1,130 +1,100 @@
1
- require "cache_store/version"
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
- #This method is called to set a value within this cache store by it's key.
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
- #This method is called to get a value from this cache store by it's unique key.
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
- # This method is called to remove a value from this cache store by it's unique key.
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
- # This method is called to check if a value exists within this cache store for a specific key.
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
- #This class is used to implement a local in memory cache store.
41
+ # Implements a local in-memory cache store.
45
42
  class LocalCacheStore
46
-
47
- attr_accessor :store
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
- #This method is called to set a value within this cache store by it's key.
47
+ # Stores a value in this cache store by its key.
55
48
  #
56
- # @param key [String] This is the unique key to reference the value being set within this cache store.
57
- # @param value [Object] This is the value to set within this cache store.
58
- # @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
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.utc + expires_in
54
+ expires = Time.now + expires_in
64
55
  end
65
- @store.push({ key: build_key(key), value: value, expires: expires})
56
+ @store.store(key, {value: value, expires: expires})
66
57
  end
67
58
 
68
- #This method is called to get a value from this cache store by it's unique key.
59
+ # Gets a value from this cache store by its unique key.
69
60
  #
70
- # @param key [String] This is the unique key to reference the value to fetch from within this cache store.
71
- # @param expires_in [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.)
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, &block)
75
-
76
- #look for the cache item in the store
77
- items = @store.select { |i| i[:key] == build_key(key) }
78
- item = if !items.empty? then items[0] else nil end
79
- #check if a valid item was found in the store
80
- if item == nil || (item[:expires] != nil && item[:expires] <= Time.now.utc)
81
- #a valid item wasn't found so check if a hydration block was specified.
82
- if block_given?
83
- #create the item from the block
84
- value = yield
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
- return nil
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
- # This method is called to remove a value from this cache store by it's unique key.
86
+ # Removes a value from this cache store by its unique key.
105
87
  #
106
- # @param key [String] This is the unique key to reference the value to remove from this cache store.
88
+ # @param key [String] The unique key to remove from this cache store.
107
89
  def remove(key)
108
- @store.delete_if { |i| i[:key] == build_key(key) }
90
+ @store.delete key
109
91
  end
110
92
 
111
- # This method is called to check if a value exists within this cache store for a specific key.
93
+ # Checks if a value exists within this cache store for a specific key.
112
94
  #
113
- # @param key [String] This is the unique key to reference the value to check for within this cache store.
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
- !@store.select { |i| i[:key] == build_key(key) }.empty?
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
@@ -1,3 +1,3 @@
1
1
  module CacheStore
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
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
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: 2016-05-18 00:00:00.000000000 Z
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: