Class: Redpear::Store::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/redpear/store/base.rb

Direct Known Subclasses

Enumerable, Lock, Value

Constant Summary collapse

IS_NIL =

Transforamtions

->v { v.nil? }.freeze
IS_ZERO =
->v { v.zero? }.freeze
IS_ONE =
->v { v == 1 }.freeze
IS_TRUE =
->v { !!v }.freeze
TO_INT =
->v { v.to_i }.freeze
TO_SET =
->v { v.to_set }.freeze
PICK_FIRST =
->v { v.first }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, conn) ⇒ Base

Constructor

Parameters:

  • key (String)

    The storage key

  • conn (Redis)

    The connection



46
47
48
# File 'lib/redpear/store/base.rb', line 46

def initialize(key, conn)
  @key, @conn = key, conn
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



14
15
16
# File 'lib/redpear/store/base.rb', line 14

def conn
  @conn
end

#keyObject (readonly) Also known as: to_s

Returns the value of attribute key.



14
15
16
# File 'lib/redpear/store/base.rb', line 14

def key
  @key
end

Class Method Details

.temporary(conn, options = {}) {|Redpear::Store::Base| ... } ⇒ Object

Creates and yields over a temporary key. Useful in combination with e.g. ‘interstore`, `unionstore`, etc.

Parameters:

  • conn (Redis)

    The connection

  • options (Hash) (defaults to: {})

    The options hash

  • [String] (Hash)

    a customizable set of options

Yields:



30
31
32
33
34
35
36
37
38
39
# File 'lib/redpear/store/base.rb', line 30

def self.temporary(conn, options = {})
  store = nil
  while !store || store.exists?
    key   = "#{options[:prefix]}#{SecureRandom.hex(20)}"
    store = new(key, conn)
  end
  yield store
ensure
  store.purge! if store
end

Instance Method Details

#clearObject

Deletes the record and returns the value



109
110
111
112
# File 'lib/redpear/store/base.rb', line 109

def clear
  purge!
  value
end

#exists?Boolean

Returns true if the record exists.

Returns:

  • (Boolean)

    true if the record exists



57
58
59
# File 'lib/redpear/store/base.rb', line 57

def exists?
  conn.exists(key)
end

#expire(time) ⇒ Object #expire(seconds) ⇒ Object

Expires the record

Overloads:

  • #expire(time) ⇒ Object

    Parameters:

    • time (Time)

      The time to expire the record at

  • #expire(seconds) ⇒ Object

    Parameters:

    • seconds (Integer)

      Expire in ‘seconds` from now



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/redpear/store/base.rb', line 84

def expire(expiration)
  case expiration
  when Time
    expire_at(expiration)
  when Integer
    expire_in(expiration)
  when String
    expiration = Kernel::Integer(expiration) rescue nil
    expire(expiration)
  else
    false
  end
end

#expire_at(time) ⇒ Object

Expires the record

Parameters:

  • time (Time)

    The time to expire the record at



116
117
118
# File 'lib/redpear/store/base.rb', line 116

def expire_at(time)
  conn.expireat key, time.to_i
end

#expire_in(seconds) ⇒ Object

Expires the record

Parameters:

  • seconds (Integer)

    Expire in ‘seconds` from now



122
123
124
# File 'lib/redpear/store/base.rb', line 122

def expire_in(seconds)
  conn.expire key, seconds.to_i
end

#inspectString

Returns custom inspect.

Returns:

  • (String)

    custom inspect



52
53
54
# File 'lib/redpear/store/base.rb', line 52

def inspect
  "#<#{self.class.name} #{key}: #{value.inspect}>"
end

#purge!Object

Deletes the whole record



99
100
101
102
103
104
105
106
# File 'lib/redpear/store/base.rb', line 99

def purge!
  case value = conn.del(key)
  when Redis::Future
    value.instance_eval { @transformation = IS_ONE }
  else
    value == 1
  end
end

#ttlInteger

Returns remaining time-to-live in seconds (if set).

Returns:

  • (Integer)

    remaining time-to-live in seconds (if set)



69
70
71
72
# File 'lib/redpear/store/base.rb', line 69

def ttl
  value = conn.ttl(key).to_i
  value if value > -1
end

#typeString

Returns type information for this record.

Returns:

  • (String)

    type information for this record



75
76
77
# File 'lib/redpear/store/base.rb', line 75

def type
  conn.type(key).to_sym
end

#valueObject

@abstract, override in subclasses



127
128
129
# File 'lib/redpear/store/base.rb', line 127

def value
  nil
end

#watch(&block) ⇒ Boolean

Watch this key

Returns:

  • (Boolean)

    true if successful



63
64
65
66
# File 'lib/redpear/store/base.rb', line 63

def watch(&block)
  conn.watch(key, &block)
  true
end