项目开发中一些特定的数据我们不一定要关系型数据库来存储,使用非关系型数据库反而更方便读取数据,效率高,这里介绍一下在java中rides的使用
1. 导入rides所需要的相关依赖jar包(在pom文件中):
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.4.2</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.5</version></dependency>
2. 在applicationContext.xml配置文件中创建rides连接池,以及properties文件的配置:
<context:property-placeholder location="classpath:properties/*.properties"/><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><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}" /></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 {@AutowiredJedisPool jedisPool;@Overridepublic 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中):
@Overridepublic 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中的值(实现免登录效果):
@AutowiredJedisPool 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("/login.jsp");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);