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. [数据结构]迪杰斯特拉(Dijkstra)算法

    基本思想 通过Dijkstra计算图G中的最短路径时,需要指定起点vs(即从顶点vs开始计算). 此外,引进两个集合S和U.S的作用是记录已求出最短路径的顶点,而U则是记录还未求出最短路径的顶点(以及 ...

  2. Extjs4 页面加载先白屏后显示的bug解决

    通过Extjs MVC结构做好页面后,加载过程中发现,会瞬间白屏,然后呈现extjs界面的问题,当类似页面放置到iframe中时,会显得非常怪异. 可通过下图体验下. 当我单击“意见反馈”菜单,在右侧 ...

  3. Spark Streaming自定义Receivers

    自定义一个Receiver class SocketTextStreamReceiver(host: String, port: Int( extends NetworkReceiver[String ...

  4. ssh登陆过程图示

  5. Java设计模式(14)责任链模式(Chain of Responsibility模式)

    Chain of Responsibility定义:Chain of Responsibility(CoR) 是用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合, ...

  6. 安卓程序代写 网上程序代写[原]Android开发技巧--ListView

    1. ListView中元素的排序 ListView中的元素排序, 即将数据源排序即可; 给集合排序的方法 : 调用Collections的sort(list, Comparator)方法, 该方法需 ...

  7. Java中static、final、static final的区别(转)+transient

    说明:不一定准确,但是最快理解. final: final可以修饰:属性,方法,类,局部变量(方法中的变量) final修饰的属性的初始化可以在编译期,也可以在运行期,初始化后不能被改变. final ...

  8. Python中的高级turtle(海龟)作图(续)

    四.填色 color 函数有三个参数.第一个参数指定有多少红色,第二个指定有多少绿色,第三个指定有多少蓝色.比如,要得到车子的亮红色,我们用 color(1,0,0),也就是让海龟用百分之百的红色画笔 ...

  9. LintCode #2 尾部的零

    计算阶乘尾部的0的个数,初一看很简单. 先上代码 public static long GetFactorial(long n) { || n == ) ; ); } //Main方法中调用 ); ; ...

  10. unity-------------------Unity5.X 新版AssetBundle使用方案及策略

    Unity5.X 新版AssetBundle使用方案及策略   1.概览 Unity3D 5.0版本之后的AssetBundle机制和之前的4.x版本已经发生了很大的变化,一些曾经常用的流程已经不再使 ...