为了方便下载,我这里分享了自己的工具,百度链接:http://pan.baidu.com/s/1qYfQlsS 密码:kgxk
我这是redis 3.0.1 win64版的,目录结构如下:
java操作redis,需要导入jedis.jar 包,我上传了jedis-2.4.2.jar。代码上和jdbc也差不太多,下面我就直接贴代码了:
package redis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 取redis 连接池
*/
public class RedisClientPool{
public static RedisClientPool redisClientPool = getInstance();
public static JedisPool jedisPool;
public static synchronized RedisClientPool getInstance()
{
if (null == redisClientPool){
redisClientPool = new RedisClientPool();
}
return redisClientPool;
}
public RedisClientPool(){
if (null == jedisPool){
init();
}
}
/**
* 初始化Jedis
* <一句话功能简述>
* <功能详细描述>
* @return
* @see [类、类#方法、类#成员]
*/
private static JedisPoolConfig initPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 控制一个pool最多有多少个状态为idle的jedis实例
jedisPoolConfig.setMaxIdle(1000);
// 最大能够保持空闲状态的对象数
jedisPoolConfig.setMaxIdle(300);
// 超时时间
jedisPoolConfig.setMaxWaitMillis(1000);
// 在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
jedisPoolConfig.setTestOnBorrow(true);
// 在还会给pool时,是否提前进行validate操作
jedisPoolConfig.setTestOnReturn(true);
return jedisPoolConfig;
}
/**
* 初始化jedis连接池
*/
public static void init(){
JedisPoolConfig jedisPoolConfig = initPoolConfig();
String host = "localhost";
int port = 6379;
int timeout = 100000;
//String password = "@caoyn";
// 构造连接池
jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
}
public static void main(String[] args) {
RedisClientPool redisClientPool1 = getInstance();
redisClientPool1.jedisPool.getResource().append("name", "曹大达");
System.out.println("已缓存成功");
}
}
下面直接使用redis连接池:
package redis;
import redis.RedisClientPool;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import redis.clients.jedis.Jedis;
/**
* redis 操作api
*/
public class RedisClient
{
/**
* 保存数据 类型为 Map
* <一句话功能简述>
* <功能详细描述>
* @param flag
* @param mapData
* @see [类、类#方法、类#成员]
*/
public static void setMapDataToRedis(String flag,Map mapData)
{
Jedis redisClient = null;
try
{
redisClient = RedisClientPool.jedisPool.getResource();
redisClient.hmset(flag,mapData);
}
catch (Exception e)
{
// 销毁对象
RedisClientPool.jedisPool.returnBrokenResource(redisClient);
}
finally
{
// 还原到连接池
RedisClientPool.jedisPool.returnResource(redisClient);
}
}
/**
* 保存数据 类型为 key-value
* <一句话功能简述>
* <功能详细描述>
* @param flag
* @param field
* @param value
* @see [类、类#方法、类#成员]
*/
public static void setDataToRedis(String flag,String field,String value)
{
Jedis redisClient = null;
try
{
redisClient = RedisClientPool.jedisPool.getResource();
redisClient.hset(flag, field, value);
}
catch (Exception e)
{
// 销毁对象
RedisClientPool.jedisPool.returnBrokenResource(redisClient);
}
finally
{
// 还原到连接池
RedisClientPool.jedisPool.returnResource(redisClient);
}
}
/**
* 获取Map数据
* <一句话功能简述>
* <功能详细描述>
* @param flag
* @return
* @see [类、类#方法、类#成员]
*/
public static Map getMapData(String flag)
{
Map dataMap = null;
Jedis redisClient = null;
try
{
redisClient = RedisClientPool.jedisPool.getResource();
dataMap = redisClient.hgetAll(flag);
}
catch (Exception e)
{
// 销毁对象
RedisClientPool.jedisPool.returnBrokenResource(redisClient);
}
finally
{
// 还原到连接池
RedisClientPool.jedisPool.returnResource(redisClient);
}
return dataMap;
}
public static long deleteData(String flag)
{
long result = 0;
Jedis redisClient = null;
try
{
redisClient = RedisClientPool.jedisPool.getResource();
result = redisClient.del(flag);
}
catch (Exception e)
{
// 销毁对象
RedisClientPool.jedisPool.returnBrokenResource(redisClient);
}
finally
{
// 还原到连接池
RedisClientPool.jedisPool.returnResource(redisClient);
}
return result;
}
/**
* 根据key和字段获取数据
* <一句话功能简述>
* <功能详细描述>
* @param flag
* @param field
* @return
* @see [类、类#方法、类#成员]
*/
public static String getData(String flag,String field)
{
String data = null;
Jedis redisClient = null;
try
{
redisClient = RedisClientPool.jedisPool.getResource();
data = redisClient.hget(flag, field);
}
catch (Exception e)
{
// 销毁对象
RedisClientPool.jedisPool.returnBrokenResource(redisClient);
}
finally
{
// 还原到连接池
RedisClientPool.jedisPool.returnResource(redisClient);
}
return data;
}
public static void main(String[] args) throws Exception{
RedisClient.testMap();
}
public static void testList()
{
Jedis redis = RedisClientPool.jedisPool.getResource();
//hset key field value将哈希表key中的域field的值设为value。
redis.hset("table", "field1", "value1");
redis.hset("table", "field2", "value2");
redis.hset("table", "field3", "value3");
//返回哈希表key中,一个或多个给定域的值。
List list = redis.hmget("table","field1","field2","field3");
for(String tmp : list)
{
System.out.println(tmp);
}
}
public static void testMap()
{
//同时将多个field - value(域-值)对设置到哈希表key中。
Map map = new ConcurrentHashMap();
for (int i = 0;i < 10000;i++){
map.put("field"+i, "value"+i);
}
if (null != getData("table", "field1")){
deleteData("table");
}
//得到map下面的username的值
Map maps = getMapData("table");
System.out.println(maps.size());
setMapDataToRedis("table",map);
//HGETALL key返回哈希表key中,所有的域和值。
maps = getMapData("table");
System.out.println(maps.size());
}
}