Redis 簡單的使用

首先看一下redis在Spring中的配置文件redis

<!-- redis鏈接池配置 -->
<bean id="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
   <property name="maxTotal" value="60" />
   <property name="maxIdle" value="30" />
   <property name="maxWaitMillis" value="100" />
   <property name="testOnBorrow" value="false" />
</bean>
<!-- jedisCluster配置 -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
   <constructor-arg index="0">
      <set>
         <bean class="redis.clients.jedis.HostAndPort">
            <constructor-arg type="String" value="${redis0.host}" />
            <constructor-arg type="int" value="${redis0.port}" />
         </bean>
         <bean class="redis.clients.jedis.HostAndPort">
            <constructor-arg type="String" value="${redis1.host}" />
            <constructor-arg type="int" value="${redis1.port}" />
         </bean>
      </set>
   </constructor-arg>
   <constructor-arg index="1" ref="genericObjectPoolConfig" />
</bean>

開發中的redis的配置文件數據庫

ps: 相關的maven依賴能夠本身去google百度等apache

緩存的泛型類json

基本的類型緩存

public class CacheObject<T> {
   
   public Boolean  needUpdate;
    public T  data;
   public Boolean getNeedUpdate() {
      return needUpdate;
   }
   public void setNeedUpdate(Boolean needUpdate) {
      this.needUpdate = needUpdate;
   }
   public T getData() {
      return data;
   }
   public void setData(T data) {
      this.data = data;
   }
   
}

 

集合類型maven

public class CacheListObject<T> {
 public Boolean  needUpdate;
 public List<T>  data;
public Boolean getNeedUpdate() {
   return needUpdate;
}
public void setNeedUpdate(Boolean needUpdate) {
   this.needUpdate = needUpdate;
}
public List<T> getData() {
   return data;
}
public void setData(List<T> data) {
   this.data = data;
}
 
}

使用緩存以後要泛型化的方法this

public static<T>  CacheListObject<T>   getCacheListObject(String jsonResult,Class<T> clz){
      Type objectType = type(CacheListObject.class,clz);
      CacheListObject cacheListObject = gson.fromJson(jsonResult,objectType);
      return cacheListObject;
   }

   public static<T>  CacheObject<T>   getCacheObject(String jsonResult,Class<T> clz){
      Type objectType = type(CacheObject.class,clz);
      CacheObject<T> cacheObject = gson.fromJson(jsonResult,objectType);
      return cacheObject;
   }

   static ParameterizedType type(final Class raw, final Type... args) {
      return new ParameterizedType() {
         public Type getRawType() {
            return raw;
         }

         public Type[] getActualTypeArguments() {
            return args;
         }

         public Type getOwnerType() {
            return null;
         }
      };
}

 

public AccountInfo getAccountInfo(String account) throws Exception {
   AccountInfo accountInfo = null;
   String key="getAccountInfo_"+ account;
   if(jedisCluster.exists(key)){
      String value = jedisCluster.get(key);
      CacheObject<AccountInfo> cacheObject = Util.getCacheObject(value,AccountInfo.class);
      if(cacheObject.getNeedUpdate()){ // 判斷帳號是否是要更新
         accountInfo = getAccountInfoMgr(account); // 要更新的話直接從數據庫拉取最新的數據
         cacheObject.setData(accountInfo);
         cacheObject.setNeedUpdate(false);
         String jsonAccountInfo = Util.coverToJson(cacheObject);
         jedisCluster.set(key,jsonAccountInfo);
      }else{
         accountInfo = cacheObject.getData();
         logger.info("開始從緩存中取帳號信息 " + account);
      }
   }else{
       CacheObject<AccountInfo> accountInfoCacheObject = new CacheObject<AccountInfo>();
       accountInfo = getAccountInfoMgr(account);
      accountInfoCacheObject.setData(accountInfo);
      accountInfoCacheObject.setNeedUpdate(false);
      String jsonAccountInfo = Util.coverToJson(accountInfoCacheObject);
      jedisCluster.set(key,jsonAccountInfo);
   }
   return accountInfo;
}


public AccountInfo getAccountInfoMgr(String account) throws Exception {
   if (Util.isEmpty(account)) {
      throw new Exception("獲取用戶信息失敗,用戶ID不能爲空!");
   }
   Account acc = accDao.findAccount(account);
   if (acc == null) {
      throw new Exception("獲取用戶信息失敗,用戶ID不存在!" + account);
   }
   AccountInfo info = getInfo(acc);
   return info;
}
相關文章
相關標籤/搜索