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')."\n";

 

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;
}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM