cache_service 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/cache_service.gemspec +20 -0
- data/lib/cache_service.rb +16 -0
- data/lib/caches/mc_cache.rb +35 -0
- data/lib/services/cache_service.rb +37 -0
- data/lib/services/config_service.rb +30 -0
- data/lib/services/sdk_logger.rb +14 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9cf278122641042d003cd6d13271027b6f8337ef
|
4
|
+
data.tar.gz: 4069a723bfc62032f76028b9283252b48ef6882c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9c141021dfb8f81fb666c3b9590265c1bfb7485dcad8ffc59312133a4c715af11b7e369e979b25e5c5c71297bf9d0b3e5c8bbb74dcbef9adac356bb8789452f
|
7
|
+
data.tar.gz: 536c71f8c41e7f98769afe0355e1a66cf530f1ac11a011d4d915050eeca40d0f262c16a0c4db82bc550460eb30f6d2d7052a3ccc64862df7dc3256bdc0ee09af
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'cache_service'
|
3
|
+
s.version = '1.0.0'
|
4
|
+
s.summary = "A gem for cache facility"
|
5
|
+
s.description = "A gem for cache facility"
|
6
|
+
s.authors = ['Linh Chau']
|
7
|
+
s.email = 'chauhonglinh@gmail.com'
|
8
|
+
s.files = [
|
9
|
+
'./cache_service.gemspec', 'lib/cache_service.rb',
|
10
|
+
'lib/services/config_service.rb', 'lib/services/cache_service.rb', 'lib/services/sdk_logger.rb',
|
11
|
+
'lib/caches/mc_cache.rb'
|
12
|
+
]
|
13
|
+
s.homepage = 'https://github.com/linhchauatl/cache_service'
|
14
|
+
s.license = 'MIT'
|
15
|
+
s.add_runtime_dependency 'logging', '~> 0'
|
16
|
+
s.add_runtime_dependency 'activesupport'
|
17
|
+
s.add_runtime_dependency 'dalli', '~> 0'
|
18
|
+
|
19
|
+
s.add_development_dependency 'rspec', '~> 3.1'
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require_relative 'services/config_service'
|
3
|
+
|
4
|
+
def load_gem_lib(sub_path)
|
5
|
+
spec = Gem::Specification.find_by_name('cache_service')
|
6
|
+
rb_files = Dir.glob("#{spec.gem_dir}/lib/#{sub_path}/*.rb")
|
7
|
+
rb_files.each { |rb_file| require rb_file }
|
8
|
+
rescue Exception => error
|
9
|
+
# Who cares?
|
10
|
+
end
|
11
|
+
|
12
|
+
['caches', 'services'].each do |sub_path|
|
13
|
+
load_gem_lib(sub_path)
|
14
|
+
rb_files = Dir.glob("#{File.expand_path('.')}/lib/#{sub_path}/*.rb")
|
15
|
+
rb_files.each { |rb_file| require rb_file }
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'dalli'
|
2
|
+
|
3
|
+
class McCache
|
4
|
+
class << self
|
5
|
+
def client
|
6
|
+
@@client
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
config = ConfigService.load_config('memcached.yml')[ConfigService.environment]
|
11
|
+
@@client ||= Dalli::Client.new(config['host'], config)
|
12
|
+
rescue => error
|
13
|
+
SdkLogger.logger.error("McCache.initialize error: #{error.message}")
|
14
|
+
@@client ||= Dalli::Client.new('localhost:11211', { host: 'localhost:11211',namespace: 'mc_cache', compress: true })
|
15
|
+
end #initialize
|
16
|
+
|
17
|
+
# Cache API, mimics ActiveSupport::Cache::Store
|
18
|
+
# http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html
|
19
|
+
def read(key, options = {})
|
20
|
+
client.get(key, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def write(key, value, options = {})
|
24
|
+
client.set(key, value, options[:expires_in], options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(key, options = {})
|
28
|
+
deleted = read(key)
|
29
|
+
client.delete(key)
|
30
|
+
deleted
|
31
|
+
end
|
32
|
+
end #class methods
|
33
|
+
|
34
|
+
initialize
|
35
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative './config_service'
|
2
|
+
|
3
|
+
class CacheService
|
4
|
+
class << self
|
5
|
+
def cache
|
6
|
+
@@cache
|
7
|
+
end
|
8
|
+
|
9
|
+
def cache=(other_cache)
|
10
|
+
@@cache = other_cache
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
config = ConfigService.load_config('cache_config.yml')[ConfigService.environment]
|
15
|
+
# Load all the existing caches in the system, outside of the gem
|
16
|
+
Dir.glob("#{File.expand_path('.')}/caches/*.rb").each { |rb_file| require rb_file }
|
17
|
+
@@cache ||= Object.const_get(config['cache'])
|
18
|
+
SdkLogger.logger = Object.const_get(config['logger']) if config['logger'].present?
|
19
|
+
rescue => error
|
20
|
+
@@cache ||= McCache
|
21
|
+
end
|
22
|
+
|
23
|
+
def read(key, options = {})
|
24
|
+
cache.read(key, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def write(key, value, options = {})
|
28
|
+
cache.write(key, value, options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete(key, options = {})
|
32
|
+
cache.delete(key, options)
|
33
|
+
end
|
34
|
+
end #class methods
|
35
|
+
|
36
|
+
initialize
|
37
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
class ConfigService
|
5
|
+
class << self
|
6
|
+
def load_config(config_file_name)
|
7
|
+
app_root = (defined? APP_ROOT)? APP_ROOT : File.expand_path('.')
|
8
|
+
|
9
|
+
config_file = nil
|
10
|
+
['conf', 'config'].each do |sub_path|
|
11
|
+
if File.exist?("#{app_root}/#{sub_path}/#{config_file_name}")
|
12
|
+
config_file = "#{app_root}/#{sub_path}/#{config_file_name}"
|
13
|
+
break
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
raise("You must have the file #{config_file_name} in either 'conf' or 'config' directory of your application") unless config_file
|
18
|
+
|
19
|
+
YAML.load_file(config_file)
|
20
|
+
end
|
21
|
+
|
22
|
+
def environment
|
23
|
+
return Rails.env if defined? Rails
|
24
|
+
return ENV['RACK_ENV'] if ENV['RACK_ENV'].present?
|
25
|
+
'local'
|
26
|
+
rescue => error
|
27
|
+
'local'
|
28
|
+
end
|
29
|
+
end # class methods
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Linh Chau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: logging
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dalli
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
69
|
+
description: A gem for cache facility
|
70
|
+
email: chauhonglinh@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- "./cache_service.gemspec"
|
76
|
+
- lib/cache_service.rb
|
77
|
+
- lib/caches/mc_cache.rb
|
78
|
+
- lib/services/cache_service.rb
|
79
|
+
- lib/services/config_service.rb
|
80
|
+
- lib/services/sdk_logger.rb
|
81
|
+
homepage: https://github.com/linhchauatl/cache_service
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.4.3
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: A gem for cache facility
|
105
|
+
test_files: []
|