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

}

redis队列操作的更多相关文章

  1. php redis队列操作

    php redis队列操作 rpush/rpushx 有序列表操作,从队列后插入元素:lpush/lpushx 和 rpush/rpushx 的区别是插入到队列的头部,同上,'x'含义是只对已存在的 ...

  2. Redis 队列操作

    class Program { //版本2:使用Redis的客户端管理器(对象池) public static IRedisClientsManager redisClientManager = ne ...

  3. python通过连接池连接redis,操作redis队列

    在每次使用redis都进行连接的话会拉低redis的效率,都知道redis是基于内存的数据库,效率贼高,所以每次进行连接比真正使用消耗的资源和时间还多.所以为了节省资源,减少多次连接损耗,连接池的作用 ...

  4. (3)redis队列功能

    Redis队列功能介绍 List 常用命令: Blpop删除,并获得该列表中的第一元素,或阻塞,直到有一个可用 Brpop删除,并获得该列表中的最后一个元素,或阻塞,直到有一个可用 Brpoplpus ...

  5. redis队列及多线程应用

    由于xxx平台上自己的博客已经很久没更新了,一直以来都是用的印象笔记来做工作中知识的积累存根,不知不觉印象笔记里已经有了四.五百遍文章.为了从新开始能与广大攻城狮共同提高技术能力与水平,随决心另起炉灶 ...

  6. Python的Flask框架应用调用Redis队列数据的方法

    转自:http://www.jb51.net/article/86021.htm 任务异步化 打开浏览器,输入地址,按下回车,打开了页面.于是一个HTTP请求(request)就由客户端发送到服务器, ...

  7. Redis服务器操作

    [Redis服务器操作] 1.TIME 返回当前服务器时间. 2.DBSIZE 返回当前数据库的 key 的数量. 3.LASTSAVE 返回最近一次 Redis 成功将数据保存到磁盘上的时间,以 U ...

  8. 【连载】redis库存操作,分布式锁的四种实现方式[三]--基于Redis watch机制实现分布式锁

    一.redis的事务介绍 1. Redis保证一个事务中的所有命令要么都执行,要么都不执行.如果在发送EXEC命令前客户端断线了,则Redis会清空事务队列,事务中的所有命令都不会执行.而一旦客户端发 ...

  9. .NET 环境中使用RabbitMQ RabbitMQ与Redis队列对比 RabbitMQ入门与使用篇

    .NET 环境中使用RabbitMQ   在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的 ...

随机推荐

  1. BMP位图文件格式详解及编程建议

    BMP文件渊源流长,虽然对JPG.PNG等格式图像文件来说,确实有点土,但是毕竟BMP文件格式相对简单,容易理解,至于BMP众多的位图格式也不能责怪微软,主要是早期谁也没料到图片技术会发展的这么快,而 ...

  2. MSXML4 SP2 sp3安装时出错

    没有启动Windows Module Installer 服务或者windows installer服务,重启试试 Windows Installer Cleanup Tool清理早期的在选项框中找到 ...

  3. 让jQuery的ajaxFileUpload插件支持onchange事件

    ajaxFileUpload插件只能上传一次的BUG发现还不少人遇到,很不幸我也遇到的,使用后发现里面的坑还不少,在createUploadForm方法中有句 var newElement = jQu ...

  4. 计算MySQL的内存峰值公式

      -- 计算MySQL的内存峰值公式,计算所有的连接满了的情况下:select (@@key_buffer_size + @@query_cache_size + @@tmp_table_size  ...

  5. Git 基本概念

    版本控制系统 (VCS) 版本控制系统 (VCS) 是软件,帮助软件开发人员携手合作,他们的工作并保持完整的历史. 以下是VCS目标 允许开发人员同步工作. 不要覆盖对方的变化. 维护历史的每一个版本 ...

  6. PCL点云配准(1)

    在逆向工程,计算机视觉,文物数字化等领域中,由于点云的不完整,旋转错位,平移错位等,使得要得到的完整的点云就需要对局部点云进行配准,为了得到被测物体的完整数据模型,需要确定一个合适的坐标系,将从各个视 ...

  7. linux守护进程编写实践

    主要参考:http://colding.bokee.com/5277082.html (实例程序是参考这的) http://wbwk2005.blog.51cto.com/2215231/400260 ...

  8. Cisco无线控制器配置Radius

    使用Cisco无线控制器管理AP,配置Radius服务器,用于企业加密wifi的认证. 结合上一篇文档进行操作: http://www.cnblogs.com/helloworldtoyou/p/80 ...

  9. Linux操作系统的安装

    一.介绍 目的:通过本文了解并掌握Linux系统安装的过程 软件环境 Linux系统:CentOS7.3 虚拟机:VM12 主机系统:Windows8.0 二.安装虚拟机 首先,需要下载VMware ...

  10. VMWare中Linux虚拟机设置静态IP上网的设置方法

    VMWare中Linux虚拟机设置静态IP上网的设置方法 标签: vmwareLinux虚拟机securecrt静态IP上网 2016-05-18 02:30 702人阅读 评论(0) 收藏 举报   ...