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 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的 ...
随机推荐
- TypeError: decoding Unicode is not supported
在试图读取网页的时候遇到TypeError: decoding Unicode is not supported, 主要原因是返回的字符串已经是unicode类型了
- linux软件管理(六)
[教程主题]:linux软件管理 [1]软件包管理 在系统管理中,软件包的管理是最重要的,是系统管理的基础的基础, 只有我们学会软件包的管理才能谈得上其它的应用. RPM RPM是软件管理程序,提供软 ...
- JQuery Table 合并单元格-解决Bug版本
网络中提供的方法是: <script type="text/javascript"> function _w_table_rowspan(_w_table_id, _w ...
- C# BCC异或校验法
通讯协议中最后一位是校验位 采用BCC(异或校验)法 这个怎么解释? 就是 把16进制的字符串转换成byte[]数据,依次拿出来进行异或一下 然后返回的值和校验位对比下 看看对不对 例如: 23230 ...
- windows10删除开始菜单中的xbox、人脉、邮件等应用
1.右键单击PowerShell,选择“以管理员身份运行” 2.输入下面的命令回车,会列出系统中所有已安装应用列表. Get-AppxPackage -AllUsers 从列表中找到你要卸载的应用,并 ...
- CRNA的使用
为了能让更多人感受 RN 的魅力,早在几个月前 React-community 就给出了一个不错的解决方案,可以让一个完全没接触过 RN 的人,从配环境开始5分钟实现 Hello Wolrd 的编写( ...
- Windows 7 incorrectly reports "No Internet Access"
PROBLEM DESCRIPTION Windows 7 may sometimes report that it has "No Internet Access"; this ...
- SDRAM 学习笔记(三)
上图是terasic公司提供的SDRAM控制器,大部分已经封装好,我们需要修改其中部分代码,以此来实现我们自己需要的功能. 1.PLL时钟设定 首先上面的sdram_pll.v中产生上一篇博客所需要的 ...
- IDEA中maven项目导jar包太慢
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/PROGRAM_anywhere/article/details/53842058参考了网上的一些教程 ...
- C++ STL Maps
Maps定义 --> 个人理解为python的字典 C++ Maps are sorted associative containers the contian unique key/value ...