项目开发中一些特定的数据我们不一定要关系型数据库来存储,使用非关系型数据库反而更方便读取数据,效率高,这里介绍一下在java中rides的使用
1. 导入rides所需要的相关依赖jar包(在pom文件中):
<dependency>
<groupId></groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
2. 在配置文件中创建rides连接池,以及properties文件的配置:
<context:property-placeholder location="classpath:properties/*.properties"/>
<bean id="jedisPoolConfig" class="">
<property name="maxTotal" value="${}" />
<property name="maxIdle" value="${}" />
<property name="minIdle" value="${}" />
<property name="maxWaitMillis" value="${}" />
<property name="testOnBorrow" value="${}" />
<property name="testOnReturn" value="${}" />
</bean>
<bean id="jedisPool" class="">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${}" />
<constructor-arg index="2" value="${}" />
<constructor-arg index="3" value="${}" />
</bean>
=192.168.1.238
=6379
=
=5000
=300
=250
=200
=3000
=true
=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 {
@Autowired
JedisPool jedisPool;
@Override
public void register(User user) {
Jedis jedis = jedisPool.getResource();
jedis.sadd(user.getId()+":"+RedisKey.FOLLOW_USER, "3","4","5");
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) {
Jedis jedis = jedisPool.getResource();
Gson gson = new Gson();
String key = UUID.randomUUID().toString();
jedis.setex("SESSION:"+key, 60*60*24*3, gson.toJson(user1));
Cookie cookie = new Cookie("token",key);
cookie.setMaxAge(60*60*24*3);
cookie.setPath("/");
response.addCookie(cookie);
}
- 在拦截器中通过key取出rides中的值(实现免登录效果):
@Autowired
JedisPool jedisPool;
Jedis jedis = jedisPool.getResource();
Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies){
if(cookie.getName().equals("token")){
String token = cookie.getValue();
String result = jedis.get("SESSION:"+token);
if(result==null){
resp.sendRedirect("/");
return false;
}else{
Gson gson = new Gson();
User user = gson.fromJson(result, User.class);
req.getSession().setAttribute("user", user);
return true;
}
}
}
- 删除rides中的某个值,通过key删除某个值:
Jedis jedis = jedisPool.getResource();
jedis.del("SESSION:"+value);