Java使用redis實現緩存的一個小demo案例

1、要想在Java中連接Redis,並進行操作,由兩種方式,一種是spring data redis,它是由spring集成的,不支持集羣,一種是官方推薦的jedis,支持集羣,其他功能差不多一樣,這裏我們介紹jedis操作實例,首先下載好jedis-2.7.3.jar包如下圖:

在這裏插入圖片描述

2、創建redis.properties配置文件:

redis.host=127.0.0.1
redis.port=6379
redis.password=
redis.timeout=100000
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.testOnBorrow=true

在這裏插入圖片描述

3、創建Redis工具類:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import redis.clients.jedis.Jedis;

/**
 * 操作redis緩存的工具類,簡單的demo,方便入門初步理解redis
 * 
 * @author Xuan
 * 
 */
public final class RedisUtil {
	public RedisUtil() {
	}

	// 以下配置可用可不用
	private static Jedis jedisxuan;// redis實例
	private static String host;// 地址
	private static String port;// 端口
	private static String password;// 授權密碼
	private static String timeout;// 超時時間:單位ms
	private static String maxIdle;// 最大空閒數:空閒鏈接數大於maxIdle時,將進行回收
	private static String maxActive;// 最大連接數:能夠同時建立的"最大鏈接個數"
	private static String maxWait;// 最大等待時間:單位ms
	private static String testOnBorrow;// 在獲取連接時,是否驗證有效性

	// 靜態代碼塊
	static {
		// 加載properties配置文件
		Properties properties = new Properties();
		InputStream is = RedisUtil.class.getClassLoader().getResourceAsStream(
				"redis.properties");
		try {
			properties.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		host = properties.getProperty("redis.host");
		port = properties.getProperty("redis.port");
		password = properties.getProperty("redis.password");
		timeout = properties.getProperty("redis.timeout");
		maxIdle = properties.getProperty("redis.maxIdle");
		maxActive = properties.getProperty("redis.maxActive");
		maxWait = properties.getProperty("redis.maxWait");
		testOnBorrow = properties.getProperty("redis.testOnBorrow");
		// 得到Jedis實例並且設置配置
		jedisxuan = new Jedis(host, Integer.parseInt(port),
				Integer.parseInt(timeout));
	}

	/**
	 * 寫入緩存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean set(final String key, String value) {
		boolean result = false;
		try {
			jedisxuan.set(key, value);
			result = true;
		} catch (Exception e) {
			System.out.println("set cache error");
		}
		return result;
	}

	/**
	 * 讀取緩存
	 * 
	 * @param key
	 * @return
	 */
	public static Object get(final String key) {
		Object result = null;
		result = jedisxuan.get(key);
		return result;
	}

	/**
	 * 刪除key對應的value
	 * 
	 * @param key
	 */
	public static void remove(final String key) {
		if (key != null && key.length() >= 1 && !key.equals("")
				&& jedisxuan.exists(key)) {
			jedisxuan.del(key);
		}
	}

	/**
	 * 判斷緩存中是否有key對應的value
	 * 
	 * @param key
	 * @return
	 */
	public static boolean exists(final String key) {
		return jedisxuan.exists(key);
	}

	/**
	 * 寫入緩存(規定緩存時間)
	 * 
	 * @param key
	 * @param value
	 * @param expireSecond
	 * @return
	 */
	public static boolean set(final String key, String value, Long expireSecond) {
		boolean result = false;
		try {
			// NX代表不存在才set,EX代表秒,NX代表毫秒
			jedisxuan.set(key, value, "NX", "EX", expireSecond);
			result = true;
		} catch (Exception e) {
			System.out.println("set cache error");
		}
		return result;
	}
}

4、測試類:

/**
 * 測試類
 * @author Xuan
 *
 */
public class test {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
			
		// 寫入一個緩存
		boolean flag = RedisUtil.set("x", "軒");
		if (flag) {
			// 讀取緩存
			System.out.println("成功寫入緩存");
			System.out.println("正在讀取緩存......");
			String xuan = String.valueOf(RedisUtil.get("x"));
			System.out.println("你讀取的緩存爲:" + xuan);
		} else {
			System.out.println("寫入緩存失敗");
		}
		
		//寫入一個帶時間的緩存 30秒消失
		//可以自己去驗證是否正確
		boolean flag1 = RedisUtil.set("xuan", "關注我博客~",Long.parseLong("30"));
		if (flag){
			System.out.println("寫入成功");
		}
	}

}

5、遇到困難可以評論(有信必回)小軒微信17382121839。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章