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

 

posted @ 2021-01-22 18:22  然然1907  阅读(122)  评论(0编辑  收藏  举报