jedis是有界面的redis,下载安装程序
安装,一直下一步就可以.
在java中使用Jedis
第一步:引入依赖
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
第二步:使用
public static void main(String[] args) {
//创建对象
Jedis jedis=new Jedis("192.168.25.133",6379);
//设置key-value
jedis.set("yzx","zx");
//获取
String yzx = jedis.get("yzx");
System.out.println(yzx);
//关闭
jedis.close();
}
第三步:查看
第四步:优化单机版连接
每一次都去连接redis可能浪费资源
所以需要写一个连接池,在连接池获取jedis对象
JedisPool jedisPool=new JedisPool("192.168.25.133",6379);
Jedis jedis = jedisPool.getResource();
String yzx = jedis.get("yzx");
System.out.println(yzx);
jedis.close();
jedisPool.close();
第五步:连接Redis集群(写入所有的ip和端口)
Set<HostAndPort> nodes=new HashSet<>();
nodes.add(new HostAndPort("192.168.25.133",7001));
nodes.add(new HostAndPort("192.168.25.133",7002));
nodes.add(new HostAndPort("192.168.25.133",7003));
nodes.add(new HostAndPort("192.168.25.133",7004));
nodes.add(new HostAndPort("192.168.25.133",7005));
nodes.add(new HostAndPort("192.168.25.133",7006));
JedisCluster jedisCluster=new JedisCluster(nodes);
jedisCluster.set("zx","yzx");
System.out.println( jedisCluster.get("zx"));
jedisCluster.close();
第六步:因为单击版和集群版使用方法不一样,所以要面向接口开发,分别实现单机版实现类和集群版实现类。
interface
package cd.e3mall.common.jedis;
public interface JedisClient {
String set(String key, String value);
String get(String key);
Boolean exists(String key);
Long expire(String key, int seconds);
Long ttl(String key);
Long incr(String key);
Long hset(String key, String field, String value);
String hget(String key, String field);
Long hdel(String key, String... field);
}
单机版实现类
public class JedisClientPool implements JedisClient {
@Autowired
private JedisPool jedisPool;
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String result = jedis.set(key, value);
jedis.close();
return result;
}
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String result = jedis.get(key);
jedis.close();
return result;
}
@Override
public Boolean exists(String key) {
Jedis jedis = jedisPool.getResource();
Boolean result = jedis.exists(key);
jedis.close();
return result;
}
@Override
public Long expire(String key, int seconds) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, seconds);
jedis.close();
return result;
}
@Override
public Long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
@Override
public Long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public Long hset(String key, String field, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(key, field, value);
jedis.close();
return result;
}
@Override
public String hget(String key, String field) {
Jedis jedis = jedisPool.getResource();
String result = jedis.hget(key, field);
jedis.close();
return result;
}
@Override
public Long hdel(String key, String... field) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(key, field);
jedis.close();
return result;
}
}
集群版实现类
public class JedisClientCluster implements JedisClient {
@Autowired
private JedisCluster jedisCluster;
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public Boolean exists(String key) {
return jedisCluster.exists(key);
}
@Override
public Long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
}
@Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
}
@Override
public Long incr(String key) {
return jedisCluster.incr(key);
}
@Override
public Long hset(String key, String field, String value) {
return jedisCluster.hset(key, field, value);
}
@Override
public String hget(String key, String field) {
return jedisCluster.hget(key, field);
}
@Override
public Long hdel(String key, String... field) {
return jedisCluster.hdel(key, field);
}
}
将集群版和单击版配置到配置文件中,当实际开发中切换到不同的接口。
配置如图:
单击版配置:
<bean id="jedisClientPool" class="cd.e3mall.common.jedis.JedisClientPool">
<property name="jedisPool" ref="jedisPool"></property>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.25.133"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
System.out.println(applicationContext);
JedisClient client = applicationContext.getBean(JedisClient.class);
client.set("a", "1");
System.out.println(client.get("a"));
集群版配置:
<bean id="jedisClientCluster" class="cn.e3mall.common.jedis.JedisClientCluster">
<property name="jedisCluster" ref="jedisCluster"/>
</bean>
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.162"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.162"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.162"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.162"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.162"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.162"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</bean>
</set>
</constructor-arg>
</bean>
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
JedisClient client = applicationContext.getBean(JedisClient.class);
client.set("c", "2");
System.out.println(client.get("c"));
注意:在使用这个注入时,需要有getter setter方法。
<property name="jedisCluster" ref="jedisCluster"/>
项目中使用redis
案例分析:为什么用try catch(不能因为缓存处理等代码报错而影响正常数据库访问)
@Override
public List<TbContent> getContentListByCid(long cid) {
//在缓存中查找
try {
String json = jedisClient.hget("CONTENT_LIST", cid + "");
if (StringUtils.isNotBlank(json)) {
System.out.println("在Redis中查询!!!");
List<TbContent> objects = JsonUtils.jsonToList(json, TbContent.class);
return objects;
}
} catch (Exception e) {
e.printStackTrace();
}
//数据库查询
TbContentExample example = new TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(cid);
List<TbContent> list = contentMapper.selectByExampleWithBLOBs(example);
System.out.println("在数据库中查询!!!");
try {
jedisClient.hset("CONTENT_LIST", cid + "", JsonUtils.objectToJson(list));
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
实现效果,第一次加载。
第二次刷新:从第二次以后都会从redis里获取(设置redis过期时间,过半小时实现对数据库访问等)
缓存同步
当我们向数据库中新增数据,删除,修改数据都要将缓存里的数据删掉,下次再查询的时候自然会在数据库中查询,因为缓存中已经被删了。
小例子;
@Override
public E3Result addContent(TbContent content) {
//将内容数据插入到内容表
content.setCreated(new Date());
content.setUpdated(new Date());
//插入到数据库
contentMapper.insert(content);
//插完数据库清空缓存
jedisClient.hdel("CONTENT_LIST",content.getCategoryId()+"");
return E3Result.ok();
}