详情可以参考菜鸟教程
安装教程:https://www.runoob.com/redis/redis-install.html
在程序中的具体使用及帮助类
//redis类似于sp的帮助类
public class RedisUtil {
public Jedis jedis;// 非切片额客户端连接
private JedisPool jedisPool;// 非切片连接池
public ShardedJedis shardedJedis;// 切片额客户端连接
private ShardedJedisPool shardedJedisPool;// 切片连接池
private RedisUtil() {
initialPool(); //初始化非切片
initialShardedPool(); //初始化切片
shardedJedis = shardedJedisPool.getResource(); //获取切片连接
jedis = jedisPool.getResource(); //获取非切片连接
}
/**
* 初始化切片池
*/
private void initialShardedPool() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(false);
// slave链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("127.0.0.1", 6379, "master")); //连接当前本机电脑及端口
// 构造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}
/**
* 初始化非切片池
*/
private void initialPool() {
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000l);
config.setTestOnBorrow(false);
jedisPool = new JedisPool(config, "127.0.0.1", 6379); //本机及端口
}
private static volatile RedisUtil redisUtil = null;
public static RedisUtil getInstance() { //单例模式
synchronized (RedisUtil.class) {
if (null == redisUtil) {
synchronized (RedisUtil.class) {
if (null == redisUtil) {
redisUtil = new RedisUtil();
}
}
}
}
return redisUtil;
}
public static final String countryTag="country_time"; //记录国家信息的存储时间
private String country_idList="country_idList";
private String country_codeList="country_codeList";
private String country_countryList="country_countryList";
private String country_countryENList="country_countryENList";
//向Redis中存入数据
public void saveCountry(List<CountryInfo> countryInfos) {
if (jedis.get(countryTag)!=null) { //说明有值,得删去原来的值
clearCountry();
}
jedis.set(countryTag, System.currentTimeMillis()+"");// 保存最新时间
for (CountryInfo countryInfo : countryInfos) {
jedis.rpush(country_idList, countryInfo.get_id()+""); //依次从右边开始加入
jedis.rpush(country_codeList, countryInfo.getUcode()+"");
jedis.rpush(country_countryList, countryInfo.getCountry());
jedis.rpush(country_countryENList, countryInfo.getCountry_EN());
}
}
//清除原有的值
private void clearCountry() {
jedis.del(country_idList,country_codeList,country_countryList,country_countryENList);
}
//从Redis中获取信息,可以降低mysql的负荷
public List<CountryInfo> getCountry() {
List<CountryInfo>list=new ArrayList<CountryInfo>();
List<String>idList=jedis.lrange(country_idList, 0, -1); //将内部数据全部存入集合中
List<String>codeList=jedis.lrange(country_codeList, 0, -1);
List<String>countryList=jedis.lrange(country_countryList, 0, -1);
List<String>countryENList=jedis.lrange(country_countryENList, 0, -1);
for(int i=0;i<idList.size();i++){
CountryInfo countryInfo=new CountryInfo(Integer.parseInt(idList.get(i)), Integer.parseInt(codeList.get(i)), countryList.get(i),
countryENList.get(i));
list.add(countryInfo);
}
return list;
}
}
Redis的使用:
//国家选项,可以弄一个Redis,暂定需要一个,看看页面里的数据是否丢失,没丢失则不需要
String time=RedisUtil.getInstance().jedis.get(RedisUtil.countryTag);
List<CountryInfo> countryInfos;
//6小时
if (time==null||(System.currentTimeMillis()-Long.parseLong(time))/1000/60/60/6>1) {
countryInfos=userService.getCountry(); //没间隔6小时,去数据库获取一次最新数据
RedisUtil.getInstance().saveCountry(countryInfos); //并将获取到的最新数据,放至Redis中
}else {
countryInfos=RedisUtil.getInstance().getCountry(); //没到6小时就从Redis中获取内容,减小数据库负荷
}