redis 数据存储怎么使用

2025-09-27 08:51:16

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

redis 数据存储怎么使用

2、 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);

}

redis 数据存储怎么使用

3、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;

redis 数据存储怎么使用

4、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);

}

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 数据存储怎么使用

6、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;

}

redis 数据存储怎么使用

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