zoukankan      html  css  js  c++  java
  • redis队列操作

    PHP版:

    <?php
    /**
     * Redis
     * 配置 $redis_host,$redis_port
     * 队列操作
     * @author win 7
     */
    class RQueue{
        
        private function __construct() {
            static $redis_host="192.168.31.200";
            static $redis_port="6379";
            static $redis_auth="";
            $this->redis = new Redis();
            
            $this->redis->connect($redis_host,$redis_port);
        }
        
        private static $_instance;
        
        public static function getInstance(){
        
            if(!(self::$_instance instanceof self)){
                self::$_instance = new self;
            }
            
            return self::$_instance;
        }
        
        public function get($qkey){
            return $this->redis->get($qkey);
        }
        public function setex($qkey, $val, $expired){
            return $this->redis->setex($qkey, $expired, $val);
        }
        /*
         * 入队操作
         */
        public function push($qkey, $content) {
            if(is_array($content))$content = json_encode($content);
            if($this->redis){
                $result =  $this->redis->lPush($qkey,$content);
                return $result;
            }else{
                return false;
            }
    
        }
        /*
         * 出队操作
        */
        public function lpop($qkey) {
            
            if($this->redis){
                $result =  $this->redis->lPop($qkey);
                return $result;
            }else{
                return false;
            }
        
        }
        
        public function close(){
            if($this->redis){
                $this->redis->close();
            }
        }
    }

    调用示例:

    $redis = RQueue::getInstance();
    $redis->setex("testredis", "abcdefg", 1000);
    $redis->push("users", array("name"=>"yangwei","age"=>23));
    echo $redis->get("testredis");
    echo $redis->lpop('users')."
    ";

    JAVA版:

    public class RedisPoolUtils {
        
        private static Logger log = Logger.getLogger(RedisPoolUtils.class.getName());
        
        static String redis_url = "";
        static int redis_port = 6379;
        static String redis_pass = "";
        
        //可用连接实例的最大数目,默认值为8;
        //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
        private static int MAX_ACTIVE = 1024;
        
        //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
        private static int MAX_IDLE = 200;
        
        //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
        private static int MAX_WAIT = 10000;
        
        private static int TIMEOUT = 10000;
        
        //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
        private static boolean TEST_ON_BORROW = true;
        
        private static JedisPool jedisPool = null;
    
        private static ResourceBundle systemModeBundle = ResourceBundle.getBundle("system_mode");
    
        private static ResourceBundle bundle = null;
        
        /**
         * 初始化Redis连接池
         */
        static {
            try {
                initDB();
                
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(MAX_ACTIVE);
                //config.setMaxActive(MAX_ACTIVE);
                config.setMaxIdle(MAX_IDLE);
                //config.setMaxWait(MAX_WAIT);
                config.setMaxWaitMillis(MAX_WAIT);
                config.setTestOnBorrow(TEST_ON_BORROW);
                jedisPool = new JedisPool(config, redis_url, redis_port, TIMEOUT, redis_pass);
                //jedisPool = new JedisPool(config, redis_url, redis_port, TIMEOUT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        
        private static void initDB(){
            String system_mode = systemModeBundle.getString("system_mode");
    
            log.info("RedisPoolUtils init system_mode :"+system_mode);
    
            if(Constants.SystemMode.PROD.getCode().equals(system_mode)){//生产模式
                bundle = ResourceBundle.getBundle("sysConfig");
            }else{//测试模式
                bundle = ResourceBundle.getBundle("sysConfig_test");
            }
            
            try {
                redis_url  = bundle.getString("redisHost");
                redis_port = Integer.valueOf(bundle.getString("redisPort"));
                redis_pass = bundle.getString("redisPass");
    
                MAX_ACTIVE = Integer.parseInt(bundle.getString("redis-max_active"));
                MAX_IDLE   = Integer.parseInt(bundle.getString("redis-max_idle"));
                MAX_WAIT   = Integer.parseInt(bundle.getString("redis-max_wait"));
                TIMEOUT    = Integer.parseInt(bundle.getString("redis-timeout"));
            }  catch  (Exception e) {    
                log.error("Get Property Exception", e);
            } finally{
            }
        }
        /**
         * 获取Jedis实例
         * @return
         */
        public  static Jedis getJedis() {
            try {
                if (jedisPool != null) {
                    Jedis resource = jedisPool.getResource();
                    return resource;
                } else {
                    return null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        
        /**
         * 释放jedis资源
         * @param jedis
         */
        public static void returnResource(final Jedis jedis) {
            if (jedis != null) {
                jedisPool.returnResourceObject(jedis);
    //            jedisPool.returnResource(jedis);
            }
        }
        
        
        public static void main(String [] args){
            Jedis jedis = RedisPoolUtils.getJedis();
            jedis.set("sms_plan", "1");
            
            String aa = jedis.get("sms_plan");
            System.out.println("sms_plan :"+aa);
            
            RedisPoolUtils.returnResource(jedis);
        }
        
        
    }
    public class RedisUtils {
        private static Logger log = Logger.getLogger(RedisUtils.class);
    
        public static String getSmsPlan() {
            String smsPlan = "1";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                smsPlan = jedis.get("sms_plan");
            } catch (Exception e) {
                log.error("redis获取smsPlan失败 ", e);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return smsPlan;
        }
    
        /**
         * 生成自增id
         * 
         * @param key
         * @return
         */
        public static Long autoIncreId(String key) {
            Long id = null;
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                id = jedis.incr("auto_id:" + key);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return id;
        }
    
        /**
         * 缓存数据
         * 
         * @param key
         * @param val
         * @return
         */
        public static String cacheData(String key, String val) {
            String ret = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                ret = jedis.set(key, val);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return ret;
        }
    
        /**
         * 获取缓存中的数据
         * 
         * @param key
         * @return
         */
        public static String getData(String key) {
            String val = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                val = jedis.get(key);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return val;
        }
    
        public static String getAccessToken(){
            String accessToken = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                accessToken = jedis.get("access_token");
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return accessToken;
        }
    
        /**
         * 缓存accessToken
         * @param val
         * @param sec 有效期
         * @return
         */
        public static String setAccessToken(String val,int sec) {
            String ret = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                ret = jedis.setex("access_token",sec,val);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return ret;
        }
    
        public static String getJsapiTicket(){
            String jsapi_ticket = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                jsapi_ticket = jedis.get("jsapi_ticket");
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return jsapi_ticket;
        }
    
        /**
         * 缓存jsapiTicket
         * @param val
         * @param sec
         * @return
         */
        public static String setJsapiTicket(String val,int sec) {
            String ret = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                ret = jedis.setex("jsapi_ticket",sec,val);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return ret;
        }
    
    
    
        /**
         * 缓存AccessToken
         * @param val
         * @return
         */
        public static String setJDAccessToken(String val) {
            String ret = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                ret = jedis.setex("jd_access_token",86400,val);
            }catch (Exception e){
                log.error("setAccessToken Exception",e);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return ret;
        }
    
        public static String getJDAccessToken(){
            String accessToken = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                accessToken = jedis.get("jd_access_token");
            } catch (Exception e){
                log.error("getAccessToken Exception",e);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return accessToken;
        }
    
        /**
         * 缓存refreshToken
         * @param val
         * @return
         */
        public static String setRefreshToken(String val) {
            String ret = "";
            Jedis jedis = null;
            try {
                jedis = RedisPoolUtils.getJedis();
                ret = jedis.set("jd_refresh_token",val);
            }catch (Exception e){
                log.error("setJDRefreshToken Exception",e);
            } finally {
                RedisPoolUtils.returnResource(jedis);
            }
            return ret;
        }

      
    /**
    * 缓存 订单id
    * @param jdOrderId
    */
    public static void addJdOrderId(String jdOrderId){
    Jedis jedis = null;
    String key = "jdOrderId";
    try {
    jedis = RedisPoolUtils.getJedis();
    jedis.lpush(key,jdOrderId);
    }catch (Exception e){
    log.error("setJDRefreshToken Exception",e);
    } finally {
    RedisPoolUtils.returnResource(jedis);
    }
    }

    /**
    * 取订单id
    */
    public static String getJdOrderId( ){
    Jedis jedis = null;
    String key = "jdOrderId";
    String jdOrderId = "";
    try {
    jedis = RedisPoolUtils.getJedis();
    boolean ret = jedis.exists(key);
    if(ret && jedis.llen(key) > 0){
    jdOrderId = jedis.rpop(key);
    }
    }catch (Exception e){
    log.error("setJDRefreshToken Exception",e);
    } finally {
    RedisPoolUtils.returnResource(jedis);
    }
    return jdOrderId;
    }

    }
  • 相关阅读:
    解决THINKCMF后台文章的相册图集只能上传一个图片的问题
    WordPress 在function.php 文件中方法中the_XXX方法失效
    Windows 增强版任务管理器-Process Explorer
    从Linux服务器下载网站文件
    SQLSERVER 免费对比数据库结构和数据的工具支持:SQL Server 2012, SQL Server 2008 and SQL Server 2005
    [UE4]蓝图继承方法:重写父类方法时,增加父类方法调用
    [UE4]蓝图使用GameMode重构
    [UE4]控制台命令,生成机器人
    [UE4]区分敌我
    [UE4]AnimOffset偏移动画
  • 原文地址:https://www.cnblogs.com/beyang/p/8302918.html
Copyright © 2011-2022 走看看