redis 数据存储怎么使用

2025-05-04 17:12:05

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

redis 数据存储怎么使用

3、2.通过key获得值的get方法: public Object get(String key) throws CoreException{ if(StringUtil.isE罪焐芡拂mpty(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; }

redis 数据存储怎么使用

5、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); } }

redis 数据存储怎么使用
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢