如1中所示,已经安装Redis及测试成功。
安装Redis图形化管理工具:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
将Redis集成到spring中
applicationcontext.xml配置 redis的javaBean
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" />
<!-- <property name="maxActive" value="600" />
<property name="maxWait" value="1000" />
<property name="testOnBorrow" value="true" /> -->
</bean>
<!-- 配置JedisPool实例 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="172.100.100.110" />
<constructor-arg index="2" value="6379" type="int" />
<constructor-arg index="3" value="600"/>
</bean>
------------以上配置是未设置redis密码的配置 如果Redis配置了密码 那么需要在以上配置中增加密码
配置完成后,在上下文中已经创建了redis的javaBean,然后将该bean注入到tokenService中 用于对token进行保存,修改,删除(redit类似数据库,内存中的数据库)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
tokenService.java文件内容
package g3.gjj.ifs;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.htf.framework.util.DateTimeUtil;
import com.htf.framework.util.StringUtils;
import com.htf.framework.webutil.token.util.TokenUtil;
public class TokenService {
//Redis池
private JedisPool jedisPool;
private int seconds = 1200; //20min
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
public String creatTokens() {
String token = TokenUtil.generateGUID(); //key
String value = String.valueOf(DateTimeUtil.getNowDateByServer()).trim();
this.setex(token, seconds, value);
return token;
}
/**
* 如果存在token 则删除该token 然后返回true
* 否则返回 false
* @param token
* @return
*/
public boolean judgeTokens(String token) {
if (StringUtils.isBlank(token)) {
return false;
}
if (this.exists(token)) {
this.del(token);
return true;
} else {
return false;
}
}
//添加
public void set(String key, String value){
Jedis jedis = this.jedisPool.getResource();
jedis.set(key, value);
}
//添加,带超时时间
public void setex(String key, int seconds, String value){
Jedis jedis = this.jedisPool.getResource();
jedis.setex(key, seconds, value);
}
//获取
public String get(String key){
Jedis jedis = this.jedisPool.getResource();
String value = jedis.get(key);
return value;
}
//查看某个键是否存在
public boolean exists(String key){
Jedis jedis = this.jedisPool.getResource();
Boolean exists = jedis.exists(key);
return exists;
}
//查看超时时间
public Long ttl(String key){
Jedis jedis = this.jedisPool.getResource();
Long ttl = jedis.ttl(key);
return ttl;
}
//删除
public void del(String key){
Jedis jedis = this.jedisPool.getResource();
jedis.del(key);
}
}
-----------------------------------------------------------
测试是否能在请求中拿到创建的token及将token保存到redis中
通过图形化工具可看到已创建了token且保存到Redis中。