Class: Redpear::Store::Base
- Inherits:
-
Object
- Object
- Redpear::Store::Base
- Defined in:
- lib/redpear/store/base.rb
Direct Known Subclasses
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
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#key ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.temporary(conn, options = {}) {|Redpear::Store::Base| ... } ⇒ Object
Creates and yields over a temporary key.
Instance Method Summary collapse
-
#clear ⇒ Object
Deletes the record and returns the value.
-
#exists? ⇒ Boolean
True if the record exists.
-
#expire(expiration) ⇒ Object
Expires the record.
-
#expire_at(time) ⇒ Object
Expires the record.
-
#expire_in(seconds) ⇒ Object
Expires the record.
-
#initialize(key, conn) ⇒ Base
constructor
Constructor.
-
#inspect ⇒ String
Custom inspect.
-
#purge! ⇒ Object
Deletes the whole record.
-
#ttl ⇒ Integer
Remaining time-to-live in seconds (if set).
-
#type ⇒ String
Type information for this record.
-
#value ⇒ Object
@abstract, override in subclasses.
-
#watch(&block) ⇒ Boolean
Watch this key.
Constructor Details
#initialize(key, conn) ⇒ Base
Constructor
46 47 48 |
# File 'lib/redpear/store/base.rb', line 46 def initialize(key, conn) @key, @conn = key, conn end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
14 15 16 |
# File 'lib/redpear/store/base.rb', line 14 def conn @conn end |
#key ⇒ Object (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.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/redpear/store/base.rb', line 30 def self.temporary(conn, = {}) store = nil while !store || store.exists? key = "#{[:prefix]}#{SecureRandom.hex(20)}" store = new(key, conn) end yield store ensure store.purge! if store end |
Instance Method Details
#clear ⇒ Object
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.
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
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
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
122 123 124 |
# File 'lib/redpear/store/base.rb', line 122 def expire_in(seconds) conn.expire key, seconds.to_i end |
#inspect ⇒ String
Returns 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 |
#ttl ⇒ Integer
Returns 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 |
#type ⇒ String
Returns type information for this record.
75 76 77 |
# File 'lib/redpear/store/base.rb', line 75 def type conn.type(key).to_sym end |
#value ⇒ Object
@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
63 64 65 66 |
# File 'lib/redpear/store/base.rb', line 63 def watch(&block) conn.watch(key, &block) true end |