Module: LightRedisCache
- Defined in:
- lib/light_redis_cache.rb,
lib/light_redis_cache/version.rb,
lib/light_redis_cache/configuration.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VERSION =
"0.1.1"
Class Attribute Summary collapse
-
.socket ⇒ Object
Returns the value of attribute socket.
Class Method Summary collapse
-
.clear ⇒ void
Clear database.
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.delete(keys) ⇒ void
Delete keys.
-
.delete_matched(matcher) ⇒ void?
Remove keys corresponding to matcher.
-
.fetch(key, expires_in: 86400, &block) ⇒ void
Get the value of a key.
-
.get(key) ⇒ String, ...
Get the value of a key.
-
.get_matched_keys(matcher) ⇒ Array<String>
Get keys corresponding to matcher.
-
.set(key, value, expires_in: 86400) ⇒ nil
Set key to hold the value.
Class Attribute Details
.socket ⇒ Object
Returns the value of attribute socket.
9 10 11 |
# File 'lib/light_redis_cache.rb', line 9 def socket @socket end |
Class Method Details
.clear ⇒ void
This method returns an undefined value.
Clear database
129 130 131 132 133 |
# File 'lib/light_redis_cache.rb', line 129 def clear open_socket @socket.puts("FLUSHALL") close_socket end |
.configuration ⇒ Object
15 16 17 |
# File 'lib/light_redis_cache.rb', line 15 def configuration @configuration ||= LightRedisCache::Configuration.new end |
.configure {|configuration| ... } ⇒ Object
11 12 13 |
# File 'lib/light_redis_cache.rb', line 11 def configure yield(configuration) end |
.delete(keys) ⇒ void
This method returns an undefined value.
Delete keys
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/light_redis_cache.rb', line 113 def delete keys open_socket request = "" request_length = 0 keys.each do |key| request.insert(-1, "$#{ key.length }\r\n#{ key }\r\n") request_length +=1 end @socket.write("*#{ request_length + 1 }\r\n$3\r\nDEL\r\n#{ request }") close_socket end |
.delete_matched(matcher) ⇒ void?
Remove keys corresponding to matcher
69 70 71 72 73 |
# File 'lib/light_redis_cache.rb', line 69 def delete_matched matcher matched_keys = get_matched_keys matcher delete matched_keys if matched_keys end |
.fetch(key, expires_in: 86400, &block) ⇒ void
This method returns an undefined value.
Get the value of a key. If the key does not exist, call a block, set the result and return it
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/light_redis_cache.rb', line 54 def fetch key, expires_in: 86400, &block result = get(key) if result == nil value = block.call set(key, value, expires_in: expires_in) value else result end end |
.get(key) ⇒ String, ...
Get the value of a key
24 25 26 27 28 29 30 |
# File 'lib/light_redis_cache.rb', line 24 def get key open_socket @socket.write("*2\r\n$3\r\nGET\r\n$#{ key.length }\r\n#{ key }\r\n") value = @socket.gets == "$-1\r\n" ? nil : JSON.parse((@socket.gets).gsub("\r\n", "").force_encoding("iso-8859-1").encode!("utf-8")) close_socket value end |
.get_matched_keys(matcher) ⇒ Array<String>
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/light_redis_cache.rb', line 88 def get_matched_keys matcher open_socket @socket.puts("KEYS #{ matcher }") first_result = @socket.gets if first_result.include?("*") matched_keys_number = first_result.gsub("*", "").gsub("\r\n", "").to_i keys = [] (1..(matched_keys_number*2)).collect do |index| if index.even? keys.push((@socket.gets).gsub("\r\n", "")) else @socket.gets end end end close_socket keys end |
.set(key, value, expires_in: 86400) ⇒ nil
Set key to hold the value
39 40 41 42 43 44 45 |
# File 'lib/light_redis_cache.rb', line 39 def set key, value, expires_in: 86400 open_socket value = value.to_json.encode("iso-8859-1").force_encoding("utf-8") @socket.write("*3\r\n$3\r\nSET\r\n$#{ key.length }\r\n#{ key }\r\n$#{ value.length }\r\n#{ value }\r\n") @socket.write("*3\r\n$6\r\nEXPIRE\r\n$#{ key.length }\r\n#{ key }\r\n$#{ expires_in.to_s.length }\r\n#{ expires_in }\r\n") close_socket end |