Redis的使用

本文详细介绍了如何在Java程序中使用Redis进行数据存储和读取,包括连接池的配置、数据操作方法以及通过Redis缓存数据以减轻数据库负担的策略。通过实例展示了如何存储和获取国家信息列表,实现数据的高效读写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

详情可以参考菜鸟教程
安装教程: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中获取内容,减小数据库负荷
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值