Module: Dialog::Storage::Redis

Defined in:
lib/storage/redis/func.rb,
lib/storage/redis/stat/push.rb,
lib/storage/redis/cached/pullpush.rb,
lib/storage/redis/cached/pushPreproc.rb

Class Method Summary collapse

Class Method Details

.pullCached(query, args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/storage/redis/cached/pullpush.rb', line 30

def pullCached(query, args)
  typeKeyDescr(query, args)        
  a =  redisDo({:type  => :cache, 
                :op    => :read, 
                :data  => {hname: "#{query[:type]}:#{query[:typeKey]}", k: query[:key]}, 
                :query => query})        
  #[0]  # possible BUG - first array element
  
  Dialog.logger.debug "Redis Request: #{query}"      
  
  if a.nil? and query[:pushCache] == true
    a = pushCachedPreproc(query, args)
  else
    Dialog.logger.debug "Using cached query for the upper request"            
  end
end

.pushCachedPreproc(query, args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/storage/redis/cached/pullpush.rb', line 48

def pushCachedPreproc(query, args)
  typeKeyDescr(query, args)
  Dialog.logger.debug "Redis pushToCache, typeKey::#{query[:typeKey]}, type::#{query[:type]}"        
  
  case query[:type]
  when 'chat'
    case query[:key]
    when 'context'
      res = pushCachedPreprocChatContext(query, args)
    end

  when 'user'
    case query[:key]
    when :example
      true
    end
  end
  
  return redisDo({:type  => :cache, 
                  :op    => :write, 
                  :data  => {hname: "#{query[:type]}:#{query[:typeKey]}", k: query[:key], v: res}, 
                  :query => query})
end

.pushCachedPreprocChatContext(query, args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/storage/redis/cached/pushPreproc.rb', line 18

def pushCachedPreprocChatContext(query, args)
  Dialog.clearApi(args)
  args[:smsg] = {chat_id: args[:mmsg][:fqndata][:o][:chat][:id]}
  args[:api][:op] = 'ChatAdministrators'
  a = Dialog::BotApi.apiActionGet(args)
  bman = 0;  cman = 0
  a['result'].each do |user|
    Dialog.logger.debug "userId #{user['user']['id']}, userStatus #{user['status']}; cliId #{args[:globals][:cliId]}, botId #{Dialog.config.naming.id}"
    cman = 1 if (user['user']['id'].to_i == args[:globals][:cliId]  and user['status'] == 'creator')
    bman = 1 if (user['user']['id'].to_i == Dialog.config.naming.id and user['status'] == 'administrator')
  end
  Dialog.logger.debug "Context args: @bman=#{bman} @cman=#{cman}"
  res = Dialog::Naming::ChatContext.Fullman if (bman == 1 and cman == 1)      
  res = Dialog::Naming::ChatContext.Botman if (bman == 1 and cman == 0)
  res = Dialog::Naming::ChatContext.Climan if (bman == 0 and cman == 1)
  res = Dialog::Naming::ChatContext.Noman if (bman == 0 and cman == 0)

  return res
end

.pushStat(query) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/storage/redis/stat/push.rb', line 19

def pushStat(query)
  Dialog.logger.debug "Redis pushToStat, typeKey::#{query[:typeKey]}, type::#{query[:type]}"        
  
  query.has_key?(:redisOp) ? op = query[:redisOp].to_sym : op = :write
  return redisDo({:type  => :stat, 
                  :op    => op, 
                  :data  => {hname: "#{query[:type]}:#{query[:typeKey]}", k: query[:key], v: query[:value]}, 
                  :query => query})
end

.redisDo(act) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/storage/redis/func.rb', line 18

def redisDo(act)
  case act[:type]
  when :cache
    redis = Dialog.redisDBCache          
  when :stat
    redis = Dialog.redisDBStat
  when :store
    redis = Dialog.redisDBStore
  end
  
  case act[:op]
  when :write
    res = redis.hmset("#{act[:data][:hname]}", ":#{act[:data][:k]}", act[:data][:v])
    act[:query].key?(:ttl) ? ttl = act[:query][:ttl] : ttl = false
    redis.expire "#{act[:data][:hname]}", act[:query][:ttl] unless ttl == false
    Dialog.logger.debug "Redis SET - #{act[:data][:hname]} K:#{act[:data][:k]} V: #{act[:data][:v]} with ttl #{ttl}"
    act[:data][:v]
  when :read
    res = redis.hmget("#{act[:query][:type]}:#{act[:query][:typeKey]}", ":#{act[:query][:key]}")[0]  # possible BUG - first array element 
  when :inc
    res = redis.hincrby("#{act[:data][:hname]}", ":#{act[:data][:k]}", 1)
  end
  
  return res
end

.typeKeyDescr(query, args) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/storage/redis/cached/pullpush.rb', line 19

def typeKeyDescr(query, args)
  case query[:type]
  when 'chat'
    query[:typeKey] = args[:mmsg][:fqndata][:o][:chat][:id]
    query[:pushCache] = true if query[:key] == 'context'
  when 'user'
    query[:typeKey] = args[:mmsg][:fqndata][:o][:from][:id]
  end
end