Class: Asynchronic::DataStore::Redis

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/asynchronic/data_store/redis.rb

Constant Summary collapse

LOCKED =
'locked'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#clear, #each, #lazy, #lazy?, #merge, #no_lazy, #readonly, #readonly?, #scoped

Constructor Details

#initialize(scope, options = {}) ⇒ Redis

Returns a new instance of Redis.



13
14
15
16
# File 'lib/asynchronic/data_store/redis.rb', line 13

def initialize(scope, options={})
  @scope = Key[scope]
  @options = options
end

Class Method Details

.connect(*args) ⇒ Object



9
10
11
# File 'lib/asynchronic/data_store/redis.rb', line 9

def self.connect(*args)
  new(*args)
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/asynchronic/data_store/redis.rb', line 18

def [](key)
  value = redis.call! 'GET', scope[key]
  value ? Marshal.load(value) : nil
rescue => ex
  Asynchronic.logger.warn('Asynchronic') { ex.message }
  value
end

#[]=(key, value) ⇒ Object



26
27
28
# File 'lib/asynchronic/data_store/redis.rb', line 26

def []=(key, value)
  redis.call! 'SET', scope[key], Marshal.dump(value)
end

#connection_argsObject



52
53
54
# File 'lib/asynchronic/data_store/redis.rb', line 52

def connection_args
  [scope, options]
end

#delete(key) ⇒ Object



30
31
32
# File 'lib/asynchronic/data_store/redis.rb', line 30

def delete(key)
  redis.call! 'DEL', scope[key]
end

#delete_cascade(key) ⇒ Object



34
35
36
37
# File 'lib/asynchronic/data_store/redis.rb', line 34

def delete_cascade(key)
  redis.call! 'DEL', scope[key]
  redis.call!('KEYS', scope[key]['*']).each { |k| redis.call! 'DEL', k }
end

#keysObject



39
40
41
# File 'lib/asynchronic/data_store/redis.rb', line 39

def keys
  redis.call!('KEYS', scope['*']).map { |k| Key[k].remove_first }
end

#synchronize(key) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/asynchronic/data_store/redis.rb', line 43

def synchronize(key)
  while redis.call!('GETSET', scope[key][LOCKED], LOCKED) == LOCKED
    sleep Asynchronic.redis_data_store_sync_timeout
  end
  yield
ensure
  redis.call! 'DEL', scope[key][LOCKED]
end