如1中所示,已經安裝Redis及測試成功。java
安裝Redis圖形化管理工具:web
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------redis
將Redis集成到spring中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>app
------------以上配置是未設置redis密碼的配置 若是Redis配置了密碼 那麼須要在以上配置中增長密碼工具
配置完成後,在上下文中已經建立了redis的javaBean,而後將該bean注入到tokenService中 用於對token進行保存,修改,刪除(redit相似數據庫,內存中的數據庫)測試
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------this
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中。