1.在电脑上安装window版本的redis
windows版:https://github.com/mythz/redis-windows
2:解压并启动redis服务。
cmd到解压目录,执行
C:\Users\yangfeng\Downloads\redis-windows-master\downloads\redis-latest>redis-se
rver.exe redis.windows.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.503 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 12676
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[12676] 31 Jul 16:51:17.134 # Server started, Redis version 3.0.503
[12676] 31 Jul 16:51:17.138 * The server is now ready to accept connections on p
ort 6379
3:另开一个cmd窗口,测试下。
C:\Users\yangfeng\Downloads\redis-windows-master\downloads\redis-latest>redis-cli
127.0.0.1:6379> set userName huangbaokang
OK
127.0.0.1:6379> get userName
"huangbaokang"
127.0.0.1:6379>
4.新建普通java工程,在src下建立redis.properties文件。
redis.pool.maxActive=100
redis.pool.maxIdle=20
redis.pool.maxWait=3000
redis.ip=localhost
redis.port=6379
5.客户端模拟类
package com.hbk.domain;
import java.util.ResourceBundle;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import com.alibaba.fastjson.JSON;
public class RedisClient {
public static JedisPool jedisPool;// 池化管理jedis链接池
static {
ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
String ip = resourceBundle.getString("redis.ip");
int port = Integer.parseInt(resourceBundle.getString("redis.port"));
JedisPoolConfig config = new JedisPoolConfig();
// 设置最大连接数
config.setMaxTotal(maxActive);
// 设置最大空闲数
config.setMaxIdle(maxIdle);
// 设置超时时间
config.setMaxWaitMillis(maxWait);
// 初始化连接池
jedisPool = new JedisPool(config, ip, port);
}
/**
* 向缓存中设置字符串内容
*/
public static boolean set(String key, String value) throws Exception {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
jedisPool.returnResource(jedis);
}
}
public static boolean set(String key, Object value) {
Jedis jedis = null;
try {
String objectJson = JSON.toJSONString(value);
jedis = jedisPool.getResource();
jedis.set(key, objectJson);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
jedisPool.returnResource(jedis);
}
}
/**
* 删除缓存中得对象,根据key
*
* @param key
* @return
*/
public static boolean del(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
jedisPool.returnResource(jedis);
}
}
/**
* 根据key 获取内容
*
* @param key
* @return
*/
public static Object get(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Object value = jedis.get(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
jedisPool.returnResource(jedis);
}
}
/**
* 根据key 获取对象
*
* @param key
* @return
*/
public static <T> T get(String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String value = jedis.get(key);
return JSON.parseObject(value, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
jedisPool.returnResource(jedis);
}
}
}
6.业务模拟对象
package com.hbk.domain;
/**
* 用户实体类
*
* @author 黄宝康
*/
public class UserVo {
private int id;
private boolean sex;
private String name;
private String phone;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "UserVo [id=" + id + ", sex=" + sex + ", name=" + name + ", phone=" + phone + ", email=" + email + "]";
}
}
7.测试类
package com.hbk.test;
import org.junit.Test;
import com.hbk.domain.RedisClient;
import com.hbk.domain.UserVo;
public class RedisDemoTest {
/*
* 向Redis中添加需要缓存的数据
*/
@Test
public void setUserCache() {
// 向缓冲中添加保存对象
UserVo user1 = new UserVo();
user1.setId(1);
user1.setSex(true);
user1.setPhone("1783797435");
user1.setEmail("gfdsgjg@163.com");
// 调用方法处理
boolean resultCache = RedisClient.set("user1", user1);
if (resultCache) {
System.out.println("向缓存中保存对象成功。");
} else {
System.out.println("向缓存中保存对象失败。");
}
}
/*
* 从Reids中获取缓存的数据
*/
@Test
public void getUserCahce() {
UserVo user1 = RedisClient.get("user1", UserVo.class);
if (user1 != null) {
System.out.println(user1.toString());
}
}
}
8.测试结果:
这里只是简单的设置并获取,jedis还提供了非常多的操作,这里就不一一举例了,请参考相关api自学。