module Dialog::Storage::Redis

Public Class Methods

pullCached(query, args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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