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);//<!--定時對線程池中空閒的鏈接進行validateObject校驗-->
		// 構造池
		//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<String> popQueue(String queueName, int count) {
		Jedis jedis = AppRedis.getJedis();
		try {
			List<String> list = new LinkedList<String>();
			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);
		};
		
		
		
	}
}


發佈了48 篇原創文章 · 獲贊 19 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章