cache_store_redis 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/cache_store_redis/version.rb +3 -0
- data/lib/cache_store_redis.rb +96 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 92f703da03a8da304163a24e31b71fe1379736af
|
4
|
+
data.tar.gz: d683eefcaec661c61fbf96b8f39a3c523b8ebf91
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a5d9f6ad0c5be2f20be8f1f7570a609aad4dd466d39b8199111e962c5cfb77df79d0fb69d964cabadd3083d2cf9c11cfd6b2b3aab8b04cce435c0d057b9caf1
|
7
|
+
data.tar.gz: 0ce6f8ba58c4bf58d7b25b68d2e7c863eb93916a98d7477f58a029e924ee9314f950d481885486dec2ec780ca6ec002d9837935e73d1f3a7b4905dd6994ae6a6
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cache_store"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "cache_store_redis/version"
|
2
|
+
require 'redis'
|
3
|
+
require 'oj'
|
4
|
+
|
5
|
+
#This class is used to implement a redis cache store.
|
6
|
+
#This class is used for interacting with a redis based cache store.
|
7
|
+
class RedisCacheStore
|
8
|
+
|
9
|
+
def initialize(namespace = nil)
|
10
|
+
@namespace = namespace
|
11
|
+
@client = Redis.new
|
12
|
+
end
|
13
|
+
|
14
|
+
#This method is called to configure the connection to the cache store.
|
15
|
+
def configure(host = 'localhost', port = 6379, db = 'default', password = nil)
|
16
|
+
config = { :host => host, :port => port, :db => db }
|
17
|
+
if password != nil
|
18
|
+
config[:password] = password
|
19
|
+
end
|
20
|
+
@client = Redis.new(config)
|
21
|
+
end
|
22
|
+
|
23
|
+
#This method is called to set a value within this cache store by it's key.
|
24
|
+
#
|
25
|
+
# @param key [String] This is the unique key to reference the value being set within this cache store.
|
26
|
+
# @param value [Object] This is the value to set within this cache store.
|
27
|
+
# @param expires_in [Integer] This is the number of seconds from the current time that this value should expire.
|
28
|
+
def set(key, value, expires_in = 0)
|
29
|
+
v = nil
|
30
|
+
k = build_key(key)
|
31
|
+
|
32
|
+
if value != nil
|
33
|
+
v = Oj.dump(value)
|
34
|
+
end
|
35
|
+
|
36
|
+
@client.set(k, v)
|
37
|
+
|
38
|
+
if expires_in > 0
|
39
|
+
@client.expire(k, expires_in)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
#This method is called to get a value from this cache store by it's unique key.
|
45
|
+
#
|
46
|
+
# @param key [String] This is the unique key to reference the value to fetch from within this cache store.
|
47
|
+
# @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.)
|
48
|
+
# @param &block [Block] This block is provided to hydrate this cache store with the value for the request key when it is not found.
|
49
|
+
# @return [Object] The value for the specified unique key within the cache store.
|
50
|
+
def get(key, expires_in = 0, &block)
|
51
|
+
|
52
|
+
k = build_key(key)
|
53
|
+
|
54
|
+
value = @client.get(k)
|
55
|
+
value = Oj.load(value) unless value == nil
|
56
|
+
|
57
|
+
if value.nil? && block_given?
|
58
|
+
value = yield
|
59
|
+
set(k, value, expires_in)
|
60
|
+
end
|
61
|
+
|
62
|
+
return value
|
63
|
+
end
|
64
|
+
|
65
|
+
# This method is called to remove a value from this cache store by it's unique key.
|
66
|
+
#
|
67
|
+
# @param key [String] This is the unique key to reference the value to remove from this cache store.
|
68
|
+
def remove(key)
|
69
|
+
|
70
|
+
@client.del(build_key(key))
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
# This method is called to check if a value exists within this cache store for a specific key.
|
75
|
+
#
|
76
|
+
# @param key [String] This is the unique key to reference the value to check for within this cache store.
|
77
|
+
# @return [Boolean] True or False to specify if the key exists in the cache store.
|
78
|
+
def exist?(key)
|
79
|
+
|
80
|
+
@client.exists(build_key(key))
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def build_key(key)
|
87
|
+
k = ''
|
88
|
+
if @namespace != nil
|
89
|
+
k = @namespace + ':' + key.to_s
|
90
|
+
elsif
|
91
|
+
k = key.to_s
|
92
|
+
end
|
93
|
+
k
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_store_redis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vaughanbrittonsage
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: redis
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: oj
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: This is the redis cache_store implementation.
|
98
|
+
email:
|
99
|
+
- vaughan.britton@sage.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- bin/console
|
105
|
+
- bin/setup
|
106
|
+
- lib/cache_store_redis.rb
|
107
|
+
- lib/cache_store_redis/version.rb
|
108
|
+
homepage: https://github.com/vaughanbrittonsage/cache_store
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.5.1
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: This is the redis cache_store implementation.
|
132
|
+
test_files: []
|
133
|
+
has_rdoc:
|