• redis缓存层实现redission


    在调用数据库mapper层之前,增加一个缓存层。

    先让实体类实现2个key的返回接口

    public interface EntityInterface {
    
      Integer getKeyId();
    
      String getKeyName();
    }

    原调用mapper的改为调用这个类

    @Service("CacheDecorator")
    @Scope("prototype")
    public class CacheDecoratorImpl<T extends EntityInterface,U extends EntityMapper> implements CacheDecorator { @Autowired RedisUtil redisUtil; private static final String ID_NAME_CACHE = "_id_name"; @Autowired private U entityMapper; @Override public T getById(Integer id) { if (id == null) { return null; } String metaName = getTClass(); Object keyName = redisUtil.getMapField(metaName + ID_NAME_CACHE, id.toString()); if (keyName != null) { Object obj = redisUtil.getMapField(metaName, keyName.toString()); if (obj != null) { return (T) obj; } } else { EntityInterface Entity = entityMapper.getById(id); if (Entity != null) { redisUtil.getMap(metaName).put(Entity.getKeyName(), Entity); redisUtil.getMap(metaName + ID_NAME_CACHE).put(id.toString(), Entity.getKeyName()); } return (T) Entity; } return null; } @Override public T getByName(String name) { if (name == null) { return null; } String metaName = getTClass(); Object obj = redisUtil.getMapField(metaName, name); if (obj != null) { return (T) obj; } else { EntityInterface e = entityMapper.getByName(name); if (e != null) { redisUtil.getMap(metaName).put(name, e); redisUtil.getMap(metaName + ID_NAME_CACHE).put(e.getKeyId().toString(), e.getKeyName()); } return (T) e; } } @Override public void update(EntityInterface Entity) { entityMapper.update(Entity); String metaName = getTClass(); redisUtil.getMap(metaName).remove(Entity.getKeyName()); redisUtil.getMap(metaName + ID_NAME_CACHE).remove(Entity.getKeyId().toString()); } @Override public void deleteById(Integer id) { entityMapper.deleteById(id); String metaName = getTClass(); redisUtil.getMap(metaName + ID_NAME_CACHE).remove(id.toString()); } @Override public void deleteByName(String name) { entityMapper.deleteByName(name); String metaName = getTClass(); redisUtil.getMap(metaName).remove(name); } @Override public void invalidate(String name) { String metaName = getTClass(); redisUtil.getMap(metaName).remove(name); } @Override public void invalidate(Integer id) { String metaName = getTClass(); redisUtil.getMap(metaName + ID_NAME_CACHE).remove(id.toString()); } public String getTClass() { String name = ""; try { InvocationHandler h = Proxy.getInvocationHandler(entityMapper); Field f = h.getClass().getDeclaredField("mapperInterface"); f.setAccessible(true); name = f.get(h).toString(); } catch (Exception e) { } return name; }

     自定义注入方式

    @Component
    public class CacheDecoratorConfig {
    
      CacheDecorator<JobMeta, JobMetaMapper> jobCacheDecorator;
    
      CacheDecoratorConfig(CacheDecorator<JobMeta, JobMetaMapper> jobCacheDecorator) {
        cacheDecorator.setMapper(indicatorMetaMapper);
        this.jobCacheDecorator = jobCacheDecorator;
      }
    
      @Bean("jobCacheDecorator")
      public CacheDecorator<JobMeta, JobMetaMapper> getJobCacheDecorator() {
        return jobCacheDecorator;
      }
    }

    实际使用方原写法:

    @Autowired
    AMapper aMapper;

    现在改为:


    @Resource
    CacheDecorator<AMeta, AMapper> jobCacheDecorator;

    调用时原写法:

    aMapper.getById(id);

    现在改为:

    jobCacheDecorator.getById(id);

  • 相关阅读:
    Java基础知识【上】(转载)
    Windows下Python中pip安装Pillow报错总结(转载)
    CentOS7下安装Python的pip
    CentOS7安装NodeJS6.9
    PostGIS(解压版)安装
    CentOS7中安装Python3.5
    CentOS7安装docker
    Centos7更改默认启动模式(转载)
    CentOS7 查看IP、Gateway、DNS、Hostname
    CentOS7系统安装及初始化
  • 原文地址:https://www.cnblogs.com/lixiaoran/p/14314736.html
Copyright © 2020-2023  润新知