java:
package com.redis; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import redis.clients.jedis.Jedis; public class RedisUtil { // redis-server.exe redis.windows.conf --->启动 redis // redis-cli.exe -h 127.0.0.1 -p 6379 ---->操作 redis public RedisUtil() { } private static Jedis jedis;// 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_config.properties"); try { properties.load(is); } catch (IOException e) { 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实例并且设置配置 jedis = 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 { jedis.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); System.out.println("set cache error"); } return result ; } /** * 读取缓存 * @param key * @return */ public static Object get(final String key) { Object result =null; result = jedis.get(key); return result; } /** * 删除key对应的value * @param key */ public static void remove(final String key) { if(key!=null&&key.length()>=1&&!key.equals("")&&jedis.exists(key)){ jedis.del(key); } } /** * 判断缓存中是否有key对应的value * @param key * @return */ public static boolean exists(final String key) { return jedis.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代表毫秒 jedis.set(key, value, "NX", "EX", expireSecond); result = true; } catch (Exception e) { e.printStackTrace(); System.out.println("set cache error on time"); } return result; } }
测试:
package com.redis; public class test { public static void main(String[] args) { //写入一个缓存 boolean result = RedisUtil.set("test", "test"); boolean result0 = RedisUtil.set("测试", "中文"); boolean result2 = RedisUtil.set("L", "LF"); if(result){ System.out.println("成功写入缓存"); System.out.println("正在读取缓存......"); String lf = String .valueOf(RedisUtil.get("测试")); System.out.println("你读取的缓存为:" + lf); }else System.out.println("写入缓存失败"); //写入一个带时间的缓存 30秒消失 boolean result1 = RedisUtil.set("l", "lf", Long.parseLong("30")); if(result0) System.out.println("成功写入缓存"); else System.out.println("写入缓存失败"); String l = (String) RedisUtil.get("L"); String l2 = (String) RedisUtil.get("测试"); String l3 = (String) RedisUtil.get("name"); String l4 = (String) RedisUtil.get("test"); System.out.println(l); System.out.println(l2); System.out.println(l3); System.out.println(l4); } }
配置文件:
路径:src/main/resources/redis_config.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