java redis remove_java操作redis、jedisUtil

1、导入jedis-2.1.0.jar包

2、java代码:

package com.mx.util;

import java.util.LinkedList;

import java.util.List;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

public class AppRedis {

private final static int RandomTimeOut = 60 * 10;

private final static int RandomTimeOut2 = 24 * 60 * 60;

private final static int USERID_TIMEOUT = 30 * 24 * 60 * 60;

private final static int RandomFreq = 60;

private final static int MaxRandomNum = 10;

private final static String dbpwd = AppStaticVar.REDIS_PWD;

private static JedisPool appJedisPool;

static {

if (appJedisPool == null) {

appJedisPool = newJedisPool();

}

}

public static Jedis getJedis() {

Jedis edis=appJedisPool.getResource();

if(dbpwd!=null&&!dbpwd.equals(""))

edis.auth(dbpwd);

//edis.select(0);//放置redis数据库

return edis;

}

public static void removeJedis(Jedis jedis) {

appJedisPool.returnResource(jedis);

}

private synchronized static JedisPool newJedisPool() {

// 池基本配置

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxActive(1000);//

config.setMaxWait(1000);//

config.setMaxIdle(20);//

config.setTestWhileIdle(true);//

// 构造池

//return new JedisPool(config, "192.168.1.95", 63791);

return new JedisPool(config, AppStaticVar.REDIS_URL, AppStaticVar.REDIS_PORT);

}

// 获取随机码的发送频率

public static String getRandomcodefreq(String usertel) {

Jedis jedis = AppRedis.getJedis();

try {

String RandomCode = jedis.get("randomcodefreq" + usertel);

if (RandomCode == null) {

return null;

}

return RandomCode;

} finally {

AppRedis.removeJedis(jedis);

}

}

// 设置获取随机码的发送频率

public static void setRandomcodefreq(String usertel) {

Jedis jedis = AppRedis.getJedis();

try {

String skey = "randomcodefreq" + usertel;

jedis.set(skey, "1");

jedis.expire(skey, RandomFreq);

} finally {

AppRedis.removeJedis(jedis);

}

}

// 获取客户的随机码

public static String getRandomCode(String usertel) {

Jedis jedis = AppRedis.getJedis();

try {

String RandomCode = jedis.get("randomcode" + usertel);

if (RandomCode == null) {

return null;

}

return RandomCode;

} finally {

AppRedis.removeJedis(jedis);

}

}

// 设置客户的随机码有效时间

public static void setRandomCode(String usertel, String randomCode) {

Jedis jedis = AppRedis.getJedis();

try {

String skey = "randomcode" + usertel;

jedis.set(skey, randomCode);

jedis.expire(skey, RandomTimeOut);

} finally {

AppRedis.removeJedis(jedis);

}

}

// 设置客户的验证码次数

public static void setRandomCodeNum(String usertel,Boolean bEvilLogin){

Jedis jedis = AppRedis.getJedis();

try {

int cnt = -1;

//如果登陆成功了,重置登陆失败次数

if(bEvilLogin){

String count = jedis.get(usertel+"num");

cnt = 1;

if (count != null) {

cnt = Integer.parseInt(count);

cnt++;

}

}else{

cnt = 0;

}

jedis.set(usertel+"num",String.valueOf(cnt));

jedis.expire(usertel+"num", RandomTimeOut2);

} finally {

AppRedis.removeJedis(jedis);

}

}

//查看客户的验证码次数是否超过10次

public static boolean checkRandomCodeNum(String usertel){

Jedis jedis = AppRedis.getJedis();

try {

String count = jedis.get(usertel+"num");

if(count != null){

int cnt = Integer.parseInt(count);

if(cnt > MaxRandomNum){

return true;

}

}

return false;

} finally {

AppRedis.removeJedis(jedis);

}

}

// 获取用户Id

public static String getUserId(String loginName) {

Jedis jedis = AppRedis.getJedis();

try {

String userId = jedis.get(loginName);

if (null == userId) {

return null;

}

return userId;

} finally {

AppRedis.removeJedis(jedis);

}

}

// 保存用户id

public static void setUserId(String loginName, String userId) {

Jedis jedis = AppRedis.getJedis();

try {

jedis.set(loginName, userId);

jedis.expire(loginName, USERID_TIMEOUT);

} finally {

AppRedis.removeJedis(jedis);

}

}

/**

* 保存值到redis中

* @author 向蓬

* @date 2015-8-26下午4:22:44

* @param key

* @param value

* @param time

* void

*/

public static void setAttrRedis(String key, String value, int time) {

Jedis jedis = AppRedis.getJedis();

try {

jedis.set(key, value);

jedis.expire(key, time);

} finally {

AppRedis.removeJedis(jedis);

}

}

/**

* 根据键获取redis中的值

* @author 向蓬

* @date 2015-8-26下午4:24:58

* @param key

* @return

* String

*/

public static String getAttrRedis(String key) {

Jedis jedis = AppRedis.getJedis();

String value = null;

try {

value = jedis.get(key);

} finally {

AppRedis.removeJedis(jedis);

}

return value;

}

/**

* 保存值到redis中(同步)

* @author 向蓬

* @date 2015-8-26下午4:22:44

* @param key

* @param value

* @param time

* void

*/

public static synchronized void setAttrRedisSyd(String key, String value, int time) {

Jedis jedis = AppRedis.getJedis();

try {

jedis.set(key, value);

jedis.expire(key, time);

} finally {

AppRedis.removeJedis(jedis);

}

}

/**

* 根据键获取redis中的值(同步)

* @author 向蓬

* @date 2015-8-26下午4:24:58

* @param key

* @return

* String

*/

public static synchronized String getAttrRedisSyd(String key) {

Jedis jedis = AppRedis.getJedis();

String value = null;

try {

value = jedis.get(key);

} finally {

AppRedis.removeJedis(jedis);

}

return value;

}

/**

* 根据键删除指定缓存数据

* @author 向蓬

* @date 2015-9-10上午10:22:36

* @param key

* @return

* long

*/

public static long delAttrRedis(String key) {

Jedis jedis = AppRedis.getJedis();

try {

return jedis.del(key);

} finally {

AppRedis.removeJedis(jedis);

}

}

/**

* 往队列里添加元素

* @author 向蓬

* @date 2016-1-14下午2:11:43

* @param queueName

* @param strings

* @return

* long

*/

public static long pushQueue(String queueName, String...strings) {

Jedis jedis = AppRedis.getJedis();

try {

return jedis.rpush(queueName, strings);

} finally {

AppRedis.removeJedis(jedis);

}

}

/**

* 按队列名取元素

* @author 向蓬

* @date 2016-1-14下午2:12:00

* @param queueName

* @return

* String

*/

public static List popQueue(String queueName, int count) {

Jedis jedis = AppRedis.getJedis();

try {

List list = new LinkedList();

for (int i = 0; i < count; i++) {

String value = jedis.lpop(queueName);

if (!StringUtil.isEmpty(value)){

list.add(value);

}

}

return list;

} finally {

AppRedis.removeJedis(jedis);

}

}

public static void main(String[] args) {

//AppRedis.delAttrRedis(StaticVar.MESSAGE_QUEUE_NAME_1);

/*MessageCmd sellMessage = new MessageCmd();

sellMessage.setMsgType(2);

sellMessage.setPopup(4);

sellMessage.setBannerId(55);

sellMessage.setFromUserId(3);

//计算卖卡收益金额

double sellProfit = DoubleUtil.mul(1, 6.0);

sellMessage.setMessage("又卖出"+1+"张卡啦!小金库里又增添了¥"+sellProfit+"元哟~");

sellMessage.setToUserId(276);

sellMessage.setSendTime(20160114160711L);

//将待处理的消息添加到redis队列

JSONObject messageJo = JSONObject.fromObject(sellMessage);

AppRedis.pushQueue(StaticVar.MESSAGE_QUEUE_NAME_1, messageJo.toString());*/

/*for (String str : AppRedis.popQueue("xp", 2)) {

System.out.println("============="+str+"出列!=============");

}*/

System.out.println("=============队列中所有消息=============");

for (String str : AppRedis.getJedis().lrange(StaticVar.MESSAGE_QUEUE_NAME_1, 0, -1)) {

System.out.println(str);

};

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值