版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 https://blog.csdn.net/weixin_43863007/article/details/88749144
项目开发中一些特定的数据我们不一定要关系型数据库来存储,使用非关系型数据库反而更方便读取数据,效率高,这里介绍一下在java中rides的使用
1. 导入rides所需要的相关依赖jar包(在pom文件中):
<!-- jedis工具包 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
<!-- gson依赖,用于把对象存在redis中转换格式-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
2. 在applicationContext.xml配置文件中创建rides连接池,以及properties文件的配置:
<!-- 读取properties配置文件的标签,因为多个文件使用的是一个IOC容器,所以只要在一个文件中读取就可以 -->
<context:property-placeholder location="classpath:properties/*.properties"/>
<!-- 配置redis的相关信息 -->
<!-- Redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="minIdle" value="${redis.pool.minIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
</bean>
<!-- 在java中需要调用redis的地方注入jedisPool这个连接池对象 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.hostName}" />
<constructor-arg index="2" value="${redis.port}" />
<constructor-arg index="3" value="${redis.timeout}" />
<!--Redis密码 -->
<!--<constructor-arg index="4" value="${redis.password}" /> -->
</bean>
redis.hostName=192.168.1.238
redis.port=6379
redis.password=
redis.timeout=5000
redis.pool.maxActive=300
redis.pool.maxIdle=250
redis.pool.minIdle=200
redis.pool.maxWait=3000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
3. 创建一个工具类用来存放可能用到的属性等信息:
/**用来存用户的点赞信息等属性 */
public class RedisKey {
//我关注的人的列表
public static final String FOLLOW_USER = "follow_user";
//关注我的人的列表
public static final String FOLLOWED_USER = "followed_user";
//我关注的话题列表
public static final String FOLLOW_TOPIC = "follow_topic";
//我关注的问题列表
public static final String FOLLOW_QUESTION = "follow_question";
//获得收藏的次数
public static final String COLLECTION_COUNT = "collection_count";
//获得点赞的次数
public static final String ZAN_COUNT = "zan_count";
}
4. rides在java项目中的使用(rides存储对象和实现用户免登陆效果):
- 创建一个rides连接池对象Jedis,只用@Autowired
- 使用rides存放用户对象信息下面是一个新增用户功能的实现,用到了工具类:通常,点赞,关注人数,收藏数这些信息可以使用rides来存储
@Service
public class UserServiceImp extends BaseServiceImpl<User> implements UserService {
//注入一个redis的连接池对象
@Autowired
JedisPool jedisPool;
@Override
public void register(User user) {
//初始化redis中的个人信息数据
//集合类型可以用 11231:follow_user 112 1132 233
//通过连接池对象获得一个连接,相当于从连接池中拿一个连接
Jedis jedis = jedisPool.getResource();//创建一个rides连接对象
//存关注的人等集合信息使用sadd()
jedis.sadd(user.getId()+":"+RedisKey.FOLLOW_USER, "3","4","5");
//存的集合默认null值不适合初始化
//存点赞次数集合,数值类型也用string,收藏次数默认值都是“0”
jedis.set(user.getId()+":"+RedisKey.ZAN_COUNT, "0");
jedis.set(user.getId()+":"+RedisKey.COLLECTION_COUNT, "0");
}
}
- 将用户对象转换成gson格式的字符串存到rides中(登录的时候把对象信息存到rides中,可以将rides的key存到cookie中):
@Override
public int selectByEmailaPwdaState(User user,HttpSession httpsession,HttpServletResponse response) {
//登录成功后将对象存到redis中,转换成gson字符串格式,cookie从redis中获取对象
//通过连接池对象获得一个连接,相当于从连接池中拿一个连接
Jedis jedis = jedisPool.getResource();
Gson gson = new Gson();//创建gson对象
//key用随机字符串将转化成json格式的对象存入redis
String key = UUID.randomUUID().toString();
jedis.setex("SESSION:"+key, 60*60*24*3, gson.toJson(user1));
//登录成功后存入cookie,实现免登陆效果
//token授权的意思,key随机字符用来调用redis中对象字符串
Cookie cookie = new Cookie("token",key);//创建cookie,里面存redis的key
cookie.setMaxAge(60*60*24*3);//设置cookie的失效时间
cookie.setPath("/");//设置cookie作用域在当前项目下
response.addCookie(cookie);//写入cookie
}
- 在拦截器中通过key取出rides中的值(实现免登录效果):
//注入一个redis的连接池对象
@Autowired
JedisPool jedisPool;
//通过连接池对象获得一个连接,相当于从连接池中拿一个连接
Jedis jedis = jedisPool.getResource();
//根据cookie来判断是否登录
//得到所有的cookie
Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies){
//查看cookie中有没有叫token的授权码key,指向的是redis中登录时存的user对象
if(cookie.getName().equals("token")){
//如果有取出token中的key授权码value去核对
String token = cookie.getValue();
//在redis中看该授权码对应的信息是否正确
String result = jedis.get("SESSION:"+token);
if(result==null){
//如果在redis中没有对应的信息跳转到登录页面
resp.sendRedirect("/login.jsp");
return false;
}else{
//不等于空说明已登录,通过
//因为是免登陆效果当退出的时候session已经被清空,需要在这里重新把对象存到session
Gson gson = new Gson();
User user = gson.fromJson(result, User.class);
req.getSession().setAttribute("user", user);
return true;
}
}
}
- 删除rides中的某个值,通过key删除某个值:
//清除redis,删除掉某个key
Jedis jedis = jedisPool.getResource();
jedis.del("SESSION:"+value);