话不多说直接进入主题
因为微信小程序的请求方式,导致没法使用常规的session,而自己之前的项目中接触过redis缓存,这次微信小程序在搭建后台框架的时候选择的是redis作为缓存工具,当然三方缓存工具还有其它的,比如memcached。
1.redis简介
Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型
2.配置文件
spring.redis.host = 127.0.0.1
spring.redis.password = 123456
spring.redis.port = 6379
spring.redis.jedis.pool.max-active = 100
spring.redis.jedis.pool.max-idle = 50
spring.redis.jedis.pool.min-idle = 5
spring.redis.jedis.pool.max-wait = 5000
spring.redis.timeout = 60000
3.redis配置
package com.robin.common.jedis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @Auther: Robin
* @Description:
* @Date: Create in 10:35 2018/3/22
*/
@Configuration
@EnableCaching
public class JedisConfiguration{
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.password}")
private String password;
@Bean
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
return jedisPool;
}
}
package com.robin.common.jedis;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Set;
/**
* redicache 工具类
*
*/
@Service
public class JedisUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
private static JedisPool jedisPool = null;
private static volatile Jedis jedis = null;
public JedisUtil(){}
public static Jedis getJedis(){
if (jedis ==null){
synchronized (Jedis.class){
if (jedis ==null){
jedis = getJedisPool().getResource();
}
}
}
return jedis;
}
public static JedisPool getJedisPool(){
if (jedisPool ==null){
synchronized (JedisPool.class){
if (jedisPool==null){
jedisPool = applicationContext.getBean(JedisPool.class);
}
}
}
return jedisPool;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(JedisUtil.applicationContext == null){
JedisUtil.applicationContext = applicationContext; //初始化 spring applicationContext
}
}
/**
* 根据key查看是否存在
* @param key
* @return
*/
public static boolean hasKey(String key){
return getJedis().exists(key);
}
/**
* 设置key -value 形式数据
* @param key
* @param value
* @return
*/
public static String set(String key,String value){
String result = getJedis().set(key,value);
return result;
}
/**
* 设置 一个过期时间
* @param key
* @param value
* @param timeOut 单位秒
* @return
*/
public static String set(String key,String value,int timeOut){
return getJedis().setex(key,timeOut,value);
}
/**
* 根据key获取value
* @param key
* @return
*/
public static String getByKey(String key){
return getJedis().get(key);
}
/**
* 根据通配符获取所有匹配的key
* @param pattern
* @return
*/
public static Set<String> getKesByPattern(String pattern){
return getJedis().keys(pattern);
}
/**
* 根据key删除
* @param key
*/
public static void delByKey(String key){
getJedis().del(key);
}
/**
* 根据key获取过期时间
* @param key
* @return
*/
public static long getTimeOutByKey(String key){
return getJedis().ttl(key);
}
/**
* 清空数据 【慎用啊!】
*/
public static void flushDB(){
getJedis().flushDB();
}
/**
* 刷新过期时间
* @param key
* @param timeOut
* @return
*/
public static long refreshLiveTime(String key,int timeOut){
return getJedis().expire(key,timeOut);
}
}