Class: Redis::BaseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/base_object.rb

Overview

Defines base functionality for all redis-objects.

Direct Known Subclasses

Counter, HashKey, List, Lock, Set, SortedSet, Value

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, *args) ⇒ BaseObject

Returns a new instance of BaseObject.



4
5
6
7
8
# File 'lib/redis/base_object.rb', line 4

def initialize(key, *args)
  @key     = key.is_a?(Array) ? key.flatten.join(':') : key
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @myredis = args.first
end

Class Method Details

.expiration_filter(*names) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/redis/base_object.rb', line 26

def expiration_filter(*names)
  names.each do |name|
    if ['=', '?', '!'].include? name.to_s[-1]
      with_name = "#{name[0..-2]}_with_expiration#{name[-1]}".to_sym
      without_name = "#{name[0..-2]}_without_expiration#{name[-1]}".to_sym
    else
      with_name = "#{name}_with_expiration".to_sym
      without_name = "#{name}_without_expiration".to_sym
    end

    alias_method without_name, name

    define_method(with_name) do |*args|
      result = send(without_name, *args)
      set_expiration
      result
    end

    alias_method name, with_name
  end
end

Instance Method Details

#redisObject

Dynamically query the handle to enable resetting midstream



11
12
13
# File 'lib/redis/base_object.rb', line 11

def redis
  @myredis || ::Redis::Objects.redis
end

#set_expirationObject



17
18
19
20
21
22
23
# File 'lib/redis/base_object.rb', line 17

def set_expiration
  if !@options[:expiration].nil?
    redis.expire(@key, @options[:expiration]) if redis.ttl(@key) < 0
  elsif !@options[:expireat].nil?
    redis.expireat(@key, @options[:expireat].to_i) if redis.ttl(@key) < 0
  end
end