Class: CustomRedis
- Inherits:
-
Object
- Object
- CustomRedis
- Defined in:
- lib/custom_redis.rb,
lib/custom_redis/version.rb
Constant Summary collapse
- VERSION =
"0.1.4"
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Class Method Summary collapse
-
.cr_delete(object, custom_key) ⇒ Object
It delete in Redis a key-value as { :“model:id:method1_method2_method3” => value }.
-
.cr_eval(object, type = :string, *methods) ⇒ Object
It evals if a key-value exists in Redis Server, being inserted if It doesn’t exist It returns the value after executing all methods TODO: Allow to send a method which several params as a hash on *methods.
- .cr_flush(object, methods) ⇒ Object
-
.cr_pflush(pattern) ⇒ Object
It delete all Redis key-values which match with the search.
-
.my_redis_key(model, string) ⇒ Object
It returns a string like model:id:method1_method2_method3.
Instance Method Summary collapse
-
#initialize(host = 'localhost', port = 6379) ⇒ CustomRedis
constructor
A new instance of CustomRedis.
Constructor Details
#initialize(host = 'localhost', port = 6379) ⇒ CustomRedis
Returns a new instance of CustomRedis.
14 15 16 17 18 19 |
# File 'lib/custom_redis.rb', line 14 def initialize(host = 'localhost', port = 6379) @host = host @port = port $custom_redis = Redis.new(:host => @host, :port => @port) end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/custom_redis.rb', line 6 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
7 8 9 |
# File 'lib/custom_redis.rb', line 7 def port @port end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
8 9 10 |
# File 'lib/custom_redis.rb', line 8 def redis @redis end |
Class Method Details
.cr_delete(object, custom_key) ⇒ Object
It delete in Redis a key-value as { :“model:id:method1_method2_method3” => value }
27 28 29 |
# File 'lib/custom_redis.rb', line 27 def self.cr_delete(object, custom_key) $custom_redis.del(my_redis_key(object, custom_key.to_s)) end |
.cr_eval(object, type = :string, *methods) ⇒ Object
It evals if a key-value exists in Redis Server, being inserted if It doesn’t exist It returns the value after executing all methods TODO: Allow to send a method which several params as a hash on *methods
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/custom_redis.rb', line 49 def self.cr_eval(object, type = :string, *methods) @methods_key = methods.join('_') cr_evalType(type) case @type when :string get_method = :get set_method = :set when :set get_method = :smembers set_method = :sadd when :hash get_method = :hash set_method = :set end # It returns a hash with a key or an array of keys if (value = cr_get(object, @methods_key, get_method)) && value.present? value else if methods.present? value = object.public_send(methods[0]) for i in 1..methods.size-1 value = value.public_send(methods[i]) end end # Iterate each element of a possible result for all methods of cr_eval and execute the each block of methods if @type == :set cr_iterate_set(object, value, set_method) else cr_set(object, value, set_method) end end end |
.cr_flush(object, methods) ⇒ Object
37 38 39 40 41 |
# File 'lib/custom_redis.rb', line 37 def self.cr_flush(object, methods) @methods_key = methods.join('_') list = $custom_redis.keys(my_redis_key(object,@methods_key)) cr_dlist(list) end |
.cr_pflush(pattern) ⇒ Object
It delete all Redis key-values which match with the search
32 33 34 35 |
# File 'lib/custom_redis.rb', line 32 def self.cr_pflush(pattern) list = $custom_redis.keys(pattern) cr_dlist(list) end |
.my_redis_key(model, string) ⇒ Object
It returns a string like model:id:method1_method2_method3
22 23 24 |
# File 'lib/custom_redis.rb', line 22 def self.my_redis_key(model, string) "#{model.class.to_s.downcase}:#{model.id.to_s}:#{string}" end |