打算在项目里面用redis了
基本配置可以参考: http://nutzbook.wendal.net/redis_basic/add_jars.html
至于详细用法嘛, 参考本站的源码, 因为这个站就用了redis哦
这是在兽总实现daocache的redis实现里面扒下来的,把redis的 get set方法封装了一个最简单的小工具
package com.nutztools.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtil {
private static JedisPool jedisPool;
static {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(10);
poolConfig.setMinIdle(5);
poolConfig.setMinEvictableIdleTimeMillis(60000);
poolConfig.setMaxWaitMillis(1 * 1000);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setTestOnBorrow(false);
jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379);
}
public static String get(String domain, String key) {
String obj = null;
try (Jedis jedis = jedisPool.getResource()) {
obj = jedis.get(domain + ":" + key);
}catch (Exception e){
e.printStackTrace();
} finally{
//jedisPool.close();
}
if (obj != null) {
return obj ;
}
return null;
}
public static boolean set(String domain, String key, String obj) {
if (obj == null) {
return false;
}
String _key = domain + ":" + key;
try (Jedis jedis = jedisPool.getResource()) {
jedis.set(_key, obj);
}catch (Exception e){
e.printStackTrace();
} finally{
//jedisPool.close();
}
return true;
}
public static void main (String [] args){
for (int i=0;i<100;i++ ){
RedisUtil.set("ab", "张", "家江阴1");
System.out.println(RedisUtil.get("seoword", "张"));
RedisUtil.set("ab", "工", "家江阴2");
System.out.println(RedisUtil.get("seoword", "工"));
RedisUtil.set("ab", "奔", "家江阴3");
System.out.println(RedisUtil.get("seoword", "奔"));
RedisUtil.set("ab", "授与封地", "家江阴4");
System.out.println(RedisUtil.get("seoword", "授与封地"));
}
}
}
package com.nutztools.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public class RedisUtil {
private static JedisPool jedisPool;static { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(10); poolConfig.setMinIdle(5); poolConfig.setMinEvictableIdleTimeMillis(60000); poolConfig.setMaxWaitMillis(1 * 1000); poolConfig.setBlockWhenExhausted(true); poolConfig.setTestOnBorrow(false); jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379); } public static String get(String domain, String key) { String obj = null; try (Jedis jedis = jedisPool.getResource()) { obj = jedis.get(domain + ":" + key); }catch (Exception e){ e.printStackTrace(); } finally{ //jedisPool.close(); } if (obj != null) { return obj ; } return null; } public static boolean set(String domain, String key, String obj) { if (obj == null) { return false; } String _key = domain + ":" + key; try (Jedis jedis = jedisPool.getResource()) { jedis.set(_key, obj); }catch (Exception e){ e.printStackTrace(); } finally{ //jedisPool.close(); } return true; }
public static void main (String [] args){
for (int i=0;i<100;i++ ){
RedisUtil.set("ab", "张", "家江阴1");
System.out.println(RedisUtil.get("seoword", "张"));
RedisUtil.set("ab", "工", "家江阴2");
System.out.println(RedisUtil.get("seoword", "工"));
RedisUtil.set("ab", "奔", "家江阴3");
System.out.println(RedisUtil.get("seoword", "奔"));
RedisUtil.set("ab", "授与封地", "家江阴4");
System.out.println(RedisUtil.get("seoword", "授与封地"));
}
}}
@hongliuliao try(....) {} 已经自动关了, jdk7语法
@wendal , 但是这样会依赖jedis的版本,2.5.0之后jedis的版本Jedis.java才有如下代码会自动:
@Override
public void close() {
if (dataSource != null) {
if (client.isBroken()) {
this.dataSource.returnBrokenResource(this);
} else {
this.dataSource.returnResource(this);
}
} else {
client.close();
}
}
try(Jedis jedis = jedisPool.getResource()) {
}
catch (Exception e){}
注意try后面的小括号,不是花括号, 这种语法已经包含了自动close
@wendal 我知道,但是如果是真的close是会有问题的,因为使用的是连接池,所以jedis在2.5.0之后的版本覆盖了close方法,并不执行真正的close,而是放回连接池
@hongliuliao 2.5.0是好久以前的版本了...
@wendal 话是这么说,不过代码能环保一些不更好么?
@hongliuliao try(....) 看着就很环保
来自炫酷的 NutzCN