Class: VisitCounter::Store::RedisStore
- Inherits:
-
AbstractStore
- Object
- AbstractStore
- VisitCounter::Store::RedisStore
- Defined in:
- lib/visit-counter/store/redis_store.rb
Constant Summary collapse
- @@redis =
nil
Class Method Summary collapse
- .acquire_lock(object) ⇒ Object
- .get(key) ⇒ Object
- .incr(key) ⇒ Object
- .lock_key(object) ⇒ Object
- .nullify(key) ⇒ Object
- .redis ⇒ Object
- .redis=(r) ⇒ Object
- .substract(key, by) ⇒ Object
- .unlock!(object) ⇒ Object
- .with_lock(object, &block) ⇒ Object
Class Method Details
.acquire_lock(object) ⇒ Object
41 42 43 |
# File 'lib/visit-counter/store/redis_store.rb', line 41 def acquire_lock(object) redis.setnx(lock_key(object), 1) end |
.get(key) ⇒ Object
29 30 31 |
# File 'lib/visit-counter/store/redis_store.rb', line 29 def get(key) redis.get(key).to_i end |
.incr(key) ⇒ Object
25 26 27 |
# File 'lib/visit-counter/store/redis_store.rb', line 25 def incr(key) redis.incr(key).to_i end |
.lock_key(object) ⇒ Object
49 50 51 |
# File 'lib/visit-counter/store/redis_store.rb', line 49 def lock_key(object) "#{object.class.name.downcase}_#{object.id}_object_cache_lock" end |
.nullify(key) ⇒ Object
33 34 35 |
# File 'lib/visit-counter/store/redis_store.rb', line 33 def nullify(key) redis.set(key, 0) end |
.redis ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/visit-counter/store/redis_store.rb', line 17 def redis if @@redis.nil? && defined?($redis) @@redis = $redis else @@redis end end |
.redis=(r) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/visit-counter/store/redis_store.rb', line 9 def redis=(r) if r.is_a?(Redis) @@redis = r else @@redis = Redis.new(r) end end |
.substract(key, by) ⇒ Object
37 38 39 |
# File 'lib/visit-counter/store/redis_store.rb', line 37 def substract(key, by) redis.decrby(key, by) end |
.unlock!(object) ⇒ Object
45 46 47 |
# File 'lib/visit-counter/store/redis_store.rb', line 45 def unlock!(object) redis.del(lock_key(object)) end |
.with_lock(object, &block) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/visit-counter/store/redis_store.rb', line 53 def with_lock(object, &block) if acquire_lock(object) begin yield ensure unlock!(object) end end end |