cache_service 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/cache_service.gemspec +2 -2
- data/lib/caches/zk_cache.rb +93 -0
- data/lib/services/cache_service.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a91fd3a3bd674fd4b38d653318909cfad705979
|
4
|
+
data.tar.gz: 79f1159e15f3f18089686f86d0a1f7e5b8fdca1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 709632fd9778adec70c52b0162e8e22fd8a2eb3d6e38e98b7b0dab360e6f7ebee07169192c9b50b1f4afa81276ee1c8666f72036747ba5bb9a6429dcf25e88ad
|
7
|
+
data.tar.gz: 0606e3b6179ae76ee513f6ff0dfa02e7113ad8b22c27a904b53bb72d0644ee0e38f4fc86882317991e761640427964c3aa1582bb18c36fd7b367443124321f50
|
data/cache_service.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'cache_service'
|
3
|
-
s.version = '1.0.
|
3
|
+
s.version = '1.0.2'
|
4
4
|
s.summary = "A gem for cache facility"
|
5
5
|
s.description = "A gem for cache facility"
|
6
6
|
s.authors = ['Linh Chau']
|
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.files = [
|
9
9
|
'./cache_service.gemspec', 'lib/cache_service.rb',
|
10
10
|
'lib/services/config_service.rb', 'lib/services/cache_service.rb', 'lib/services/sdk_logger.rb',
|
11
|
-
'lib/caches/mc_cache.rb'
|
11
|
+
'lib/caches/mc_cache.rb', 'lib/caches/zk_cache.rb'
|
12
12
|
]
|
13
13
|
s.homepage = 'https://github.com/linhchauatl/cache_service'
|
14
14
|
s.license = 'MIT'
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'zookeeper'
|
4
|
+
|
5
|
+
class ZkCache
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def client
|
9
|
+
@@client
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
config = ConfigService.load_config('zookeeper.yml')[ConfigService.environment]
|
14
|
+
@@client ||= Zookeeper.new(config['host'])
|
15
|
+
rescue => error
|
16
|
+
SdkLogger.logger.error("ZkCache.initialize error: #{error.message}")
|
17
|
+
@@client ||= Zookeeper.new('localhost:2181')
|
18
|
+
end #initialize
|
19
|
+
|
20
|
+
|
21
|
+
# Cache API, mimics ActiveSupport::Cache::Store
|
22
|
+
# http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html
|
23
|
+
def read(key, options = {})
|
24
|
+
key = process_key(key)
|
25
|
+
result = client.get(path: key)[:data]
|
26
|
+
result = decode_data(result)
|
27
|
+
|
28
|
+
return result unless result.is_a?(Hash)
|
29
|
+
|
30
|
+
sym_data = result.with_indifferent_access
|
31
|
+
return sym_data unless sym_data[:zk_cache_data].present?
|
32
|
+
|
33
|
+
sym_data = sym_data[:zk_cache_data]
|
34
|
+
return nil if expires?(sym_data)
|
35
|
+
|
36
|
+
sym_data[:value]
|
37
|
+
end
|
38
|
+
|
39
|
+
def write(key, value, options = {})
|
40
|
+
key = process_key(key)
|
41
|
+
init_key(key, nil)
|
42
|
+
written_value = value
|
43
|
+
if options[:expires_in].to_i > 0
|
44
|
+
written_value = { zk_cache_data: {
|
45
|
+
value: value,
|
46
|
+
expires_in: (options[:expires_in].to_i),
|
47
|
+
last_read: Time.now
|
48
|
+
}
|
49
|
+
}.to_json
|
50
|
+
end
|
51
|
+
|
52
|
+
client.set(path: key, data: written_value)
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete(key, options = {})
|
56
|
+
key = process_key(key)
|
57
|
+
deleted = read(key)
|
58
|
+
client.delete(path: key)
|
59
|
+
deleted
|
60
|
+
end
|
61
|
+
|
62
|
+
def decode_data(data)
|
63
|
+
ActiveSupport::JSON.decode(data)
|
64
|
+
rescue => error
|
65
|
+
data
|
66
|
+
end
|
67
|
+
|
68
|
+
def expires?(data)
|
69
|
+
return true if (data.nil? || !data.is_a?(Hash))
|
70
|
+
data = data.with_indifferent_access
|
71
|
+
return DateTime.parse(data[:last_read]).to_i + data[:expires_in].to_i < Time.now.to_i
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def init_key(key, data)
|
77
|
+
process_key(key)
|
78
|
+
|
79
|
+
if @@client.get(path: key)[:data].nil?
|
80
|
+
SdkLogger.logger.info("Create zookeeper path #{key} ...")
|
81
|
+
@@client.create(path: key, data: data)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def process_key(key)
|
86
|
+
key = "/#{key}" unless key.starts_with?('/')
|
87
|
+
key
|
88
|
+
end
|
89
|
+
|
90
|
+
end #class methods
|
91
|
+
|
92
|
+
initialize
|
93
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Linh Chau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logging
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- "./cache_service.gemspec"
|
76
76
|
- lib/cache_service.rb
|
77
77
|
- lib/caches/mc_cache.rb
|
78
|
+
- lib/caches/zk_cache.rb
|
78
79
|
- lib/services/cache_service.rb
|
79
80
|
- lib/services/config_service.rb
|
80
81
|
- lib/services/sdk_logger.rb
|