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;
}
}
redis队列操作的更多相关文章
- php redis队列操作
php redis队列操作 rpush/rpushx 有序列表操作,从队列后插入元素:lpush/lpushx 和 rpush/rpushx 的区别是插入到队列的头部,同上,'x'含义是只对已存在的 ...
- Redis 队列操作
class Program { //版本2:使用Redis的客户端管理器(对象池) public static IRedisClientsManager redisClientManager = ne ...
- python通过连接池连接redis,操作redis队列
在每次使用redis都进行连接的话会拉低redis的效率,都知道redis是基于内存的数据库,效率贼高,所以每次进行连接比真正使用消耗的资源和时间还多.所以为了节省资源,减少多次连接损耗,连接池的作用 ...
- (3)redis队列功能
Redis队列功能介绍 List 常用命令: Blpop删除,并获得该列表中的第一元素,或阻塞,直到有一个可用 Brpop删除,并获得该列表中的最后一个元素,或阻塞,直到有一个可用 Brpoplpus ...
- redis队列及多线程应用
由于xxx平台上自己的博客已经很久没更新了,一直以来都是用的印象笔记来做工作中知识的积累存根,不知不觉印象笔记里已经有了四.五百遍文章.为了从新开始能与广大攻城狮共同提高技术能力与水平,随决心另起炉灶 ...
- Python的Flask框架应用调用Redis队列数据的方法
转自:http://www.jb51.net/article/86021.htm 任务异步化 打开浏览器,输入地址,按下回车,打开了页面.于是一个HTTP请求(request)就由客户端发送到服务器, ...
- Redis服务器操作
[Redis服务器操作] 1.TIME 返回当前服务器时间. 2.DBSIZE 返回当前数据库的 key 的数量. 3.LASTSAVE 返回最近一次 Redis 成功将数据保存到磁盘上的时间,以 U ...
- 【连载】redis库存操作,分布式锁的四种实现方式[三]--基于Redis watch机制实现分布式锁
一.redis的事务介绍 1. Redis保证一个事务中的所有命令要么都执行,要么都不执行.如果在发送EXEC命令前客户端断线了,则Redis会清空事务队列,事务中的所有命令都不会执行.而一旦客户端发 ...
- .NET 环境中使用RabbitMQ RabbitMQ与Redis队列对比 RabbitMQ入门与使用篇
.NET 环境中使用RabbitMQ 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的 ...
随机推荐
- mongo 操作小结
这里总结一下mongo常用操作语句,分享给大家和我自己~ 打印系统,数据库,集合的信息 db.stats() 打印数据库状态 db ...
- C语言 · 十进制数转八进制数
算法训练 十进制数转八进制数 时间限制:1.0s 内存限制:512.0MB 编写函数把一个十进制数输出其对应的八进制数. 样例输入 9274 样例输出 22072 #includ ...
- tensorflow入门 (一)
转载:作者:地球的外星人君链接:https://www.zhihu.com/question/49909565/answer/207609620来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...
- golang sqrt error练习
练习:错误 从先前的练习中复制 Sqrt 函数,并修改使其返回 error 值. 由于不支持复数,当 Sqrt 接收到一个负数时,应当返回一个非 nil 的错误值. 创建一个新类型 type Er ...
- 根据时间获取最新数据 SQL(每一个人或者每一项)
-- 方法1 select a.* from table1 a from table1 b where b.name=a.name and b.gdtime>a.gdtime) -- 方法2 s ...
- 从商业角度探讨API设计
为Web设计.实现和维护API不仅仅是一项挑战:对很多公司来说,这是一项势在必行的任务.本系列将带领读者走过一段旅程,从为API确定业务用例到设计方法论,解决实现难题,并从长远的角度看待在Web上维护 ...
- js实现选集功能
项目中有个播放列表选集的需求,如下图: 现在展示的1-42集全部,我们如何实现这个选集的功能呢? 我的思路如下: 1.将这42集按每10集划分,并存入数组: 2.保存开始和结束位置,比如说1~10,开 ...
- greendao数据库初次使用的配置及多表关联的初始化
1.在工程外层(Project)的build.gradle中添加依赖 buildscript { repositories { jcenter() } dependencies { classpath ...
- r语言 工作空间内的对象
objects.size() objects() 脚本举例 #将以下代码粘贴到编辑器中,另存为regression.r文件. rate<-c(20, 22, 24, 26, 28, 30, 32 ...
- Sword ACE编译
1.设置环境变量 #ACE_ROOT是指ACE解压目录 export ACE_ROOT=/home/person/2/ACE_wrappers export LD_LIBRARY_PATH=$ACE_ ...