1、redis存数据(注册时将数据同时存入redis)
//存入redis缓存
redisService.initializationAccount(pd);
2、redisService中的写法
public UserAccount initializationAccount(PageData pd){
UserAccount account = new UserAccount();
try {
PageData pageData = accountService.redisAccountList(pd);
// account.setU_id(pageData.getString("u_id"));
account.setID(pageData.getString("ID"));
account.setHx_id(pageData.getString("hx_id"));
account.setNick_name(pageData.getString("nick_name"));
setAccount(pageData.getString("u_id"), account);
} catch (Exception e) {
e.printStackTrace();
}
return account;
}
A、redisAccountList数据从数据库中查询,mybatis写法如下:
<select id="RedisAccount" parameterType="pd" resultType="pd">
SELECT
IFNULL((SELECT u_id FROM bit_user WHERE u_id= #{u_id}),0) AS u_id,
IFNULL((SELECT ID FROM bit_user WHERE u_id= #{u_id}),0) AS ID,
IFNULL((SELECT hx_id FROM bit_user WHERE u_id= #{u_id}),0) AS hx_id,
IFNULL((SELECT nick_name FROM bit_user WHERE u_id= #{u_id}),0) AS nick_name
</select>
B、setAccount方法存入数据
public void setAccount(String key,UserAccount accont){
if(StringUtils.isNotBlank(key) && accont != null){
RedisUtils redisUtils = new RedisUtils(jedisPool);
redisUtils.setObject("U"+key, accont, 0);
}
}
C、setObject方法见上篇redis工具类博客中的RedisUtils类
3、redis取值:
UserAccount accont = redisService.getAccont(pd.getString("uid"));//从redis中取值
system.out.print(accont.getNick_name());//测试数据是否打印
A、getAccont方法在redisService中:
public UserAccount getAccont(String id){
UserAccount accont;
RedisUtils redisUtils = new RedisUtils(jedisPool);
accont = (UserAccount)redisUtils.getObject("U"+id);
if(accont == null ){
accont = new UserAccount();
}
return accont;
}
本文介绍了一种使用Redis进行数据缓存的方法,包括如何在注册过程中将数据存入Redis,以及如何从Redis中检索数据。文章详细展示了具体的实现代码,如初始化账户信息到Redis缓存的过程、从数据库查询数据填充Redis的步骤等。

136

被折叠的 条评论
为什么被折叠?



