使用redis存取数据

   redis是常用的缓存管理工具,因其读取内存的特性,常被高并发对性能有要求的项目所采用,这里介绍java将对象存入redis并获得该对象的常用方法。

   1.将对象以键值对形式存入redis中的set方法

/**
	 * 增加
	 * @throws Exception
	 * */
	public void set(String key,Object value) throws CoreException {
		if(StringUtil.isEmpty(key)||value==null){
			throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"key或者value不能为空"});
		}
		ShardedJedis jedis = (ShardedJedis)pool.getResource();
		try{
			jedis.set(SafeEncoder.encode(key),  SerializeUtil.serialise(value));
		}catch(Throwable e){
			throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"插入redis数据异常"});
		}finally{
			pool.returnResource(jedis);
		}
	} 

2.通过key获得值的get方法

	public Object get(String key) throws CoreException{
		if(StringUtil.isEmpty(key)){
			throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"key不能为空"});
		}
		Object obj=null;
		try{
			ShardedJedis jedis = (ShardedJedis)pool.getResource();
			try{
				byte[] object=jedis.get(SafeEncoder.encode(key));
				if(object!=null){
					obj=SerializeUtil.unserialize(object);
				}
			}catch (Throwable e) {
				e.printStackTrace();
				throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"获取redis值异常"});
			}finally{
				pool.returnResource(jedis);
			}
			if(obj==null){
				return obj;
			}
		}catch (Exception e) {
			logger.info("未找到可用的key:"+key);
			try{
				Map<String,String> configMap=redisExceptionSearchConfig.get(key);
				Object redisExceptionOprClass=appContext.getBean(configMap.get("className"));
				Method method=redisExceptionOprClass.getClass().getMethod(configMap.get("methodName"), null);
				obj=method.invoke(redisExceptionOprClass, null);
			}catch(Exception e1){
				e1.printStackTrace();
				throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"redis从数据库获取数据异常"});
			}
		}
		return obj;
	} 

3. 通过key 删除某个对象的方法

	public void delete(String key) throws CoreException{
		if(StringUtil.isEmpty(key)){
			throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"key不能为空"});
		}
		ShardedJedis jedis = (ShardedJedis)pool.getResource();
		try{
			jedis.del(SafeEncoder.encode(key));
		}catch(Throwable e){
			throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"删除redis数据异常"});
		}finally{
			pool.returnResource(jedis);
		}
	} 

4.包含关键字删除,按关键字删除一批key值包含该关键字的对象

/**
	 * 包含关键字删除
	 * @param reqkey
	 * @throws CoreException
	 */
	public void deleteByRegExp(String reqkey) throws CoreException{
		if(StringUtil.isEmpty(reqkey)){
			throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"key不能为空"});
		}
		Set<String> set =new HashSet<String>();
		ShardedJedis jedis = (ShardedJedis)this.pool.getResource();
		try{
			Collection<Jedis> allJedis = jedis.getAllShards();
			for (Jedis t : allJedis) {
		          Set<byte[]> keys = t.keys(SafeEncoder.encode("*" +reqkey + "*"));
		          for (byte[] key : keys) {
		        	  logger.info( "ContainKey:"+SafeEncoder.encode(key));
		        	  jedis.del(SafeEncoder.encode(key));
		          }
		        }
		}catch(Throwable e){
			throw new CoreException("获取包含关键字:"+reqkey+"redis异常");
		}finally{
			pool.returnResource(jedis);
		}
	} 

5. 取出包含关键字的所有key值的对象

/**
	 * 取出包含关键字所有key
	 * @param reqkey
	 * @return
	 * @throws CoreException
	 */
	public Set<String>  getDataByContainKey(String reqkey)throws CoreException{
		if(StringUtil.isEmpty(reqkey)){
			throw new CoreException(PayErrorConstant.C_PAY_BIZ00000,new Object[]{"key不能为空"});
		}
		Set<String> set =new HashSet<String>();
		ShardedJedis jedis = (ShardedJedis)this.pool.getResource();
		try{
			Collection<Jedis> allJedis = jedis.getAllShards();
			for (Jedis t : allJedis) {
		          Set<byte[]> keys = t.keys(SafeEncoder.encode("*" +reqkey + "*"));
		          for (byte[] key : keys) {
		        	  logger.info( "ContainKey:"+SafeEncoder.encode(key));
		        	  set.add(SafeEncoder.encode(key));//KEY值转换成String
		          }
		        }
		}catch(Throwable e){
			throw new CoreException("获取包含关键字:"+reqkey+"redis异常");
		}finally{
			pool.returnResource(jedis);
		}

		return set;
	}

  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

景天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值