All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.edgar615.util.spring.jdbc.CacheWrappedJdbc Maven / Gradle / Ivy

package com.github.edgar615.util.spring.jdbc;

import com.google.common.collect.Lists;

import com.github.edgar615.util.db.Jdbc;
import com.github.edgar615.util.db.Persistent;
import com.github.edgar615.util.search.Example;
import java.util.HashMap;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.List;
import java.util.Map;
import org.springframework.cache.support.NullValue;

/**
 * 改为直接在jdbcImpl上实现.
 *
 * @author Edgar  Date 2018/5/24
 */
@Deprecated
public class CacheWrappedJdbc implements Jdbc {
  private final Jdbc jdbc;

  private final CacheManager cacheManager;

  public CacheWrappedJdbc(Jdbc jdbc, CacheManager cacheManager) {
    this.jdbc = jdbc;
    this.cacheManager = cacheManager;
  }

  @Override
  public  void insert(Persistent persistent) {
    jdbc.insert(persistent);
  }

  @Override
  public  ID insertAndGeneratedKey(Persistent persistent) {
   return jdbc.insertAndGeneratedKey(persistent);
  }

  @Override
  public > void batchInsert(List persistentList) {
    jdbc.batchInsert(persistentList);
  }

  @Override
  public > int deleteById(Class elementType, ID id) {
    int result = jdbc.deleteById(elementType, id);
    evictCache(elementType, id);
    return result;
  }

  @Override
  public > int deleteByExample(Class elementType, Example example) {
    int result = jdbc.deleteByExample(elementType, example);
    clearCache(elementType);
    return result;
  }

  @Override
  public  int updateById(Persistent persistent, Map addOrSub,
                             List nullFields, ID id) {
    int result = jdbc.updateById(persistent, addOrSub, nullFields, id);
    evictCache(persistent.getClass(), id);
    return result;
  }

  @Override
  public  int updateByExample(Persistent persistent, Map addOrSub,
                                  List nullFields, Example example) {
    int result = jdbc.updateByExample(persistent, addOrSub, nullFields, example);
    clearCache(persistent.getClass());
    return result;
  }

  @Override
  public > T findById(Class elementType, ID id,
                                                   List fields) {
    Cache cache = cacheManager.getCache(cacheName(elementType));
    if (cache == null) {
      return jdbc.findById(elementType, id, fields);
    }
    T persistent = null;
    Cache.ValueWrapper valueWrapper = cache.get(id);
    if (valueWrapper != null) {
      Object value = valueWrapper.get();
      if (NullValue.INSTANCE == value) {
        return null;
      }
      persistent = (T) value;
    } else {
      persistent = jdbc.findById(elementType, id, Lists.newArrayList());
      if (persistent == null) {
        cache.put(id, NullValue.INSTANCE);
        return null;
      }
      cache.put(id, persistent);
    }
    if (fields == null || fields.isEmpty()) {
      return persistent;
    }
    Map map = persistent.toMap();
    Map newMap = new HashMap<>();
    fields.stream().forEach(f -> newMap.put(f, map.get(f)));
    Persistent newPersistent = Persistent.create(elementType);
    newPersistent.fromMap(newMap);
    return (T) newPersistent;
  }

  @Override
  public > List findByExample(Class elementType,
                                                              Example example) {
    return jdbc.findByExample(elementType, example);
  }

  @Override
  public > List findByExample(Class elementType, Example example,
                                                              int start, int limit) {
    return jdbc.findByExample(elementType, example, start, limit);
  }

  @Override
  public > int countByExample(Class elementType, Example example) {
    return jdbc.countByExample(elementType, example);
  }

  private void evictCache(Class elementType, Object id) {
    Cache cache = cacheManager.getCache(cacheName(elementType));
    if (cache != null) {
      cache.evict(id);
    }
  }

  private void clearCache(Class elementType) {
    Cache cache = cacheManager.getCache(cacheName(elementType));
    if (cache != null) {
      cache.clear();
    }
  }

  private  Persistent newDomain(Class> clazz) {
    try {
      return clazz.newInstance();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  private String cacheName(Class elementType) {
    return elementType.getSimpleName() + "JdbcCache";
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy