Springbootはredisを統合し、プロジェクトの起動時にredisキャッシュをロードします.


プロジェクトには辞書表、メカニズム表、ユーザー表があるため、これらの一般的な状況では、データが変更される確率は比較的小さく、クエリー、トランスコード、クエリーデータベースの頻度が高いため、プロジェクトの開始時にこれらのデータ表を保存にロードして使用します.
redisは高度なkey:valueストレージシステムで、valueは5つのデータ型をサポートしています.
1.文字列(strings)
2.文字列リスト(lists)
3.文字列セット(sets)
4.秩序文字列セット(sorted sets)
5.ハッシュ(hashes)
 
1.redis jarパッケージの構成
 
            org.springframework.boot
            spring-boot-starter-data-redis
 

2.ymlファイルの構成
redis:database:2 host:IPアドレスport:6379 password:1
3.キャッシュステップの初期化
redisに格納されるデータ量が大きい場合は、データ量が小さい場合はスレッドプール方式を採用することができます.いいえ
@Configuration
@EnableAsync
public class AsyncConfig {

	@Bean
	public TaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		//        
		executor.setCorePoolSize(10);
		//        
		executor.setMaxPoolSize(20);
		//       
		executor.setQueueCapacity(40);
		//         ( )
		executor.setKeepAliveSeconds(60);
		//         
		executor.setThreadNamePrefix("async-");
		//       
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		//                
		executor.setWaitForTasksToCompleteOnShutdown(true);

		return executor;
	}
}
/**
 *       
 *
 * @author 
 */
@Component
public class InitCache implements ApplicationRunner {

	@Autowired
	private ICacheService cache;

	@Override
	@Async
	public void run(ApplicationArguments arg) throws Exception {
		//        
		cache.initDictCache();
		//        
		cache.initExpressionCache();
	}
}

4.辞書表キャッシュのロード
サービスインプリメンテーションクラス
	@Override
	public void initDictCache() {
		List systemDictList = systemDictMapper.querySystemDictTable();
		if (CollectionUtils.isNotEmpty(systemDictList)) {
			for (SystemDictTableEntity systemDictTableEntity : systemDictList) {
				if (null != systemDictTableEntity) {
					if (StringUtils.isNotBlank(systemDictTableEntity.getDictValue())
							&& StringUtils.isNotBlank(systemDictTableEntity.getDictKey())) {
						RedisUtils.set(RedisKeyPrefix.REDIS_SYSTEM_DICT_KEY + systemDictTableEntity.getDictKey(),
								systemDictTableEntity.getDictValue());
					}
				}
			}
		}
	}

スレッドプール方式のキャッシュのロード:
実装クラスは、前に書いたスレッドプールに注入する構成クラスが必要です.
   @Autowired     private TaskExecutor taskExecutor;  
@Override
	public void initExpressionCache() {
		long start = System.currentTimeMillis();
		log.info("Start Loading Expression into redis cache...");

		//          
		List expressionCacheDtoList = mapper.getExpression();

		//              
		if (CollectionUtils.isEmpty(expressionCacheDtoList)) {
			log.info("There Is Nothing Need To Cache!");
			return;
		}

		//          
		int pageCount = expressionCacheDtoList.size();

		//           
		int pageSize = 300;

		//            
		int threadCount = pageCount % pageSize == ConstantNumber.ZERO ? pageCount / pageSize
				: pageCount / pageSize + ConstantNumber.ONE;

		//   threadCount   
		for (int pageNumber = 1; pageNumber <= threadCount; pageNumber++) {
			//       
			final int executeNumber = pageSize * pageNumber;
			taskExecutor.execute(() -> executeCache(expressionCacheDtoList, pageCount, pageSize, executeNumber));
		}

		//           
		double cost = (System.currentTimeMillis() - start) / 1000.000;
		log.info("Started Loading Expression Cache in {}", cost + " seconds");
	}
/**
	 *         REDIS  
	 * 
	 * @param expressionCacheDtoList
	 * @param pageCount
	 * @param pageSize
	 * @param executeNumber
	 */
	private void executeCache(List expressionCacheDtoList, int pageCount, int pageSize,
			int executeNumber) {

		//                
		executeNumber = executeNumber < pageCount ? executeNumber : pageCount;

		//     
		String expression;
		Integer columnId;
		Integer expressionTypeId;
		StringBuilder sb = new StringBuilder();

		//        
		//   Thread-1  0( )~300(  ),Thread-2  300~600(  )
		for (int j = executeNumber - pageSize; j < executeNumber; j++) {
			ExpressionCacheDto expressionCacheDto = expressionCacheDtoList.get(j);
			if (expressionCacheDto != null) {
				//   StringBuilder
				sb.delete(ConstantNumber.ZERO, sb.length());

				//     Value
				expression = expressionCacheDto.getExpression();
				columnId = expressionCacheDto.getColumnId();
				expressionTypeId = expressionCacheDto.getExpressionTypeId();

				//   RedisKey
				sb.append(RedisKeyPrefix.REDIS_EXPRESSION_KEY).append(columnId).append(UNDERLINE)
						.append(expressionTypeId);

				//     
				RedisUtils.set(sb.toString(), expression);
			} else {
				log.info("cache value is null...");
			}
		}
	}

 
5  .RedisUtilクラス
package cn.com.citydo.web.util;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * Redis     
 * 
 * @author yss
 *
 */
@Component
@Slf4j
public class RedisUtils {

	private static RedisTemplate redisTemplate;

	@Autowired
	public void setRedisTemplate(RedisTemplate redisTemplate) {
		if (null == redisTemplate) {
			log.info("Redis       ,      ");
		} else {
			log.info("Redis         !");
		}
		RedisUtils.redisTemplate = redisTemplate;
	}

	/**
	 *         
	 * 
	 * @param key
	 *             
	 * @param time
	 *              ( )
	 * @return
	 */
	public static boolean expire(String key, long time) {
		try {
			if (time > 0) {
				redisTemplate.expire(key, time, TimeUnit.SECONDS);
			}
			return true;
		} catch (Exception e) {
			log.error("expire() is error : {}", e);
			return false;
		}
	}

	/**
	 *   key       
	 * 
	 * @param key
	 *                 null
	 * @return   ( )   0       
	 */
	public static long getExpire(String key) {
		return redisTemplate.getExpire(key, TimeUnit.SECONDS);
	}

	/**
	 *   key    
	 * 
	 * @param key
	 *             
	 * @return true    false   
	 */
	public static boolean hasKey(String key) {
		try {
			return redisTemplate.hasKey(key);
		} catch (Exception e) {
			log.error("hasKey() is error : {}", e);
			return false;
		}
	}

	/**
	 *       
	 * 
	 * @param key
	 *             
	 * @return  
	 */
	public static Object get(String key) {
		return key == null ? null : redisTemplate.opsForValue().get(key);
	}

	/**
	 *       
	 * 
	 * @param key
	 *             
	 * @param value
	 *             
	 * @return true   false  
	 */
	public static boolean set(String key, Object value) {
		try {
			redisTemplate.opsForValue().set(key, value);
			return true;
		} catch (Exception e) {
			log.error("set() is error : {}", e);
			return false;
		}

	}

	/**
	 *            
	 * 
	 * @param key
	 *             
	 * @param value
	 *             
	 * @param time
	 *              ( ) time   0   time    0       
	 * @return true   false   
	 */
	public static boolean set(String key, Object value, long time) {
		try {
			if (time > 0) {
				redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
			} else {
				set(key, value);
			}
			return true;
		} catch (Exception e) {
			log.error("set() is error : {}", e);
			return false;
		}
	}

	/**
	 * HashGet
	 * 
	 * @param key
	 *                 null
	 * @param item
	 *                 null
	 * @return  
	 */
	public static Object hget(String key, String item) {
		return redisTemplate.opsForHash().get(key, item);
	}

	/**
	 *   hashKey       
	 * 
	 * @param key
	 *             
	 * @return        
	 */
	public static Map hmget(String key) {
		return redisTemplate.opsForHash().entries(key);
	}

	/**
	 * HashSet
	 * 
	 * @param key
	 *             
	 * @param map
	 *                  
	 * @return true    false   
	 */
	public static boolean hmset(String key, Map map) {
		try {
			redisTemplate.opsForHash().putAll(key, map);
			return true;
		} catch (Exception e) {
			log.error("hmset() is error : {}", e);
			return false;
		}
	}

	/**
	 * HashSet      
	 * 
	 * @param key
	 *             
	 * @param map
	 *                  
	 * @param time
	 *              ( )
	 * @return true   false  
	 */
	public static boolean hmset(String key, Map map, long time) {
		try {
			redisTemplate.opsForHash().putAll(key, map);
			if (time > 0) {
				expire(key, time);
			}
			return true;
		} catch (Exception e) {
			log.error("hmset() is error : {}", e);
			return false;
		}
	}

	/**
	 *    hash      ,        
	 * 
	 * @param key
	 *             
	 * @param item
	 *             
	 * @param value
	 *             
	 * @return true    false  
	 */
	public static boolean hset(String key, String item, Object value) {
		try {
			redisTemplate.opsForHash().put(key, item, value);
			return true;
		} catch (Exception e) {
			log.error("hset() is error : {}", e);
			return false;
		}
	}

	/**
	 *    hash      ,        
	 * 
	 * @param key
	 *             
	 * @param item
	 *             
	 * @param value
	 *             
	 * @param time
	 *              ( )   :      hash    ,           
	 * @return true    false  
	 */
	public static boolean hset(String key, String item, Object value, long time) {
		try {
			redisTemplate.opsForHash().put(key, item, value);
			if (time > 0) {
				expire(key, time);
			}
			return true;
		} catch (Exception e) {
			log.error("hset() is error : {}", e);
			return false;
		}
	}

	/**
	 *   hash    
	 * 
	 * @param key
	 *                 null
	 * @param item
	 *                       null
	 */
	public static void hdel(String key, Object... item) {
		redisTemplate.opsForHash().delete(key, item);
	}

	/**
	 *   hash         
	 * 
	 * @param key
	 *                 null
	 * @param item
	 *                 null
	 * @return true    false   
	 */
	public static boolean hHasKey(String key, String item) {
		return redisTemplate.opsForHash().hasKey(key, item);
	}

	/**
	 * hash        ,                
	 * 
	 * @param key
	 *             
	 * @param item
	 *             
	 * @param by
	 *                (  0)
	 * @return
	 */
	public static double hincr(String key, String item, double by) {
		return redisTemplate.opsForHash().increment(key, item, by);
	}

	/**
	 * hash  
	 * 
	 * @param key
	 *             
	 * @param item
	 *             
	 * @param by
	 *                (  0)
	 * @return
	 */
	public static double hdecr(String key, String item, double by) {
		return redisTemplate.opsForHash().increment(key, item, -by);
	}

	/**
	 *   key  Set     
	 * 
	 * @param key
	 *             
	 * @return
	 */
	public static Set sGet(String key) {
		try {
			return redisTemplate.opsForSet().members(key);
		} catch (Exception e) {
			log.error("sGet() is error : {}", e);
			return null;
		}
	}

	/**
	 *   value   set   ,    
	 * 
	 * @param key
	 *             
	 * @param value
	 *             
	 * @return true    false   
	 */
	public static boolean sHasKey(String key, Object value) {
		try {
			return redisTemplate.opsForSet().isMember(key, value);
		} catch (Exception e) {
			log.error("sHasKey() is error : {}", e);
			return false;
		}
	}

	/**
	 *      set  
	 * 
	 * @param key
	 *             
	 * @param values
	 *                   
	 * @return     
	 */
	public static long sSet(String key, Object... values) {
		try {
			return redisTemplate.opsForSet().add(key, values);
		} catch (Exception e) {
			log.error("sSet() is error : {}", e);
			return 0;
		}
	}

	/**
	 *  set      
	 * 
	 * @param key
	 *             
	 * @param time
	 *              ( )
	 * @param values
	 *                   
	 * @return     
	 */
	public static long sSetAndTime(String key, long time, Object... values) {
		try {
			Long count = redisTemplate.opsForSet().add(key, values);
			if (time > 0) {
				expire(key, time);
			}
			return count;
		} catch (Exception e) {
			log.error("sSetAndTime() is error : {}", e);
			return 0;
		}
	}

	/**
	 *   set     
	 * 
	 * @param key
	 *             
	 * @return
	 */
	public static long sGetSetSize(String key) {
		try {
			return redisTemplate.opsForSet().size(key);
		} catch (Exception e) {
			log.error("sGetSetSize() is error : {}", e);
			return 0;
		}
	}

	/**
	 *     value 
	 * 
	 * @param key
	 *             
	 * @param values
	 *                   
	 * @return      
	 */
	public static long setRemove(String key, Object... values) {
		try {
			Long count = redisTemplate.opsForSet().remove(key, values);
			return count;
		} catch (Exception e) {
			log.error("setRemove() is error : {}", e);
			return 0;
		}
	}

	/**
	 *   list     
	 * 
	 * @param key
	 *             
	 * @param start
	 *              
	 * @param end
	 *               0   -1     
	 * @return
	 */
	public static List lGet(String key, long start, long end) {
		try {
			return redisTemplate.opsForList().range(key, start, end);
		} catch (Exception e) {
			log.error("lGet() is error : {}", e);
			return null;
		}
	}

	/**
	 *   list     
	 * 
	 * @param key
	 *             
	 * @return
	 */
	public static List lGet(String key) {
		return lGet(key, 0, -1);
	}

	/**
	 *   list     
	 * 
	 * @param key
	 *             
	 * @return
	 */
	public static long lGetListSize(String key) {
		try {
			return redisTemplate.opsForList().size(key);
		} catch (Exception e) {
			log.error("lGetListSize() is error : {}", e);
			return 0;
		}
	}

	/**
	 *        list   
	 * 
	 * @param key
	 *             
	 * @param index
	 *               index>=0 , 0   ,1      ,    ;index<0 ,-1,  ,-2       ,    
	 * @return
	 */
	public static Object lGetIndex(String key, long index) {
		try {
			return redisTemplate.opsForList().index(key, index);
		} catch (Exception e) {
			log.error("lGetIndex() is error : {}", e);
			return null;
		}
	}

	/**
	 *  list    
	 * 
	 * @param key
	 *             
	 * @param value
	 *             
	 * @param time
	 *              ( )
	 * @return
	 */
	public static boolean lSet(String key, Object value) {
		try {
			redisTemplate.opsForList().rightPush(key, value);
			return true;
		} catch (Exception e) {
			log.error("lSet() is error : {}", e);
			return false;
		}
	}

	/**
	 *  list    
	 * 
	 * @param key
	 *             
	 * @param value
	 *             
	 * @param time
	 *              ( )
	 * @return
	 */
	public static boolean lSet(String key, Object value, long time) {
		try {
			redisTemplate.opsForList().rightPush(key, value);
			if (time > 0) {
				expire(key, time);
			}
			return true;
		} catch (Exception e) {
			log.error("lSet() is error : {}", e);
			return false;
		}
	}

	/**
	 *  list    
	 * 
	 * @param key
	 *             
	 * @param value
	 *             
	 * @param time
	 *              ( )
	 * @return
	 */
	public static boolean lSet(String key, List value) {
		try {
			redisTemplate.opsForList().rightPushAll(key, value);
			return true;
		} catch (Exception e) {
			log.error("lSet() is error : {}", e);
			return false;
		}
	}

	/**
	 *  list    
	 * 
	 * @param key
	 *             
	 * @param value
	 *             
	 * @param time
	 *              ( )
	 * @return
	 */
	public static boolean lSet(String key, List value, long time) {
		try {
			redisTemplate.opsForList().rightPushAll(key, value);
			if (time > 0) {
				expire(key, time);
			}
			return true;
		} catch (Exception e) {
			log.error("lSet() is error : {}", e);
			return false;
		}
	}

	/**
	 *       list      
	 * 
	 * @param key
	 *             
	 * @param index
	 *              
	 * @param value
	 *             
	 * @return
	 */
	public static boolean lUpdateIndex(String key, long index, Object value) {
		try {
			redisTemplate.opsForList().set(key, index, value);
			return true;
		} catch (Exception e) {
			log.error("lUpdateIndex() is error : {}", e);
			return false;
		}
	}

	/**
	 *   N   value
	 * 
	 * @param key
	 *             
	 * @param count
	 *                 
	 * @param value
	 *             
	 * @return      
	 */
	public static long lRemove(String key, long count, Object value) {
		try {
			Long remove = redisTemplate.opsForList().remove(key, count, value);
			return remove;
		} catch (Exception e) {
			log.error("lRemove() is error : {}", e);
			return 0;
		}
	}

	/**
	 * 
	 *     
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public static  T getObjectBean(String key, Class clazz) {

		String value = (String) redisTemplate.opsForValue().get(key);
		return parseJson(value, clazz);
	}

	/**
	 *     
	 * 
	 * @param key
	 * @param obj
	 */
	public static  void setObjectBean(String key, T obj) {
		if (obj == null) {
			return;
		}

		String value = toJson(obj);
		redisTemplate.opsForValue().set(key, value);
	}

	/**
	 *           
	 * 
	 * @param key
	 * @param obj
	 */
	public static  T getAndSet(String key, T obj, Class clazz) {
		if (obj == null) {
			return getObjectBean(key, clazz);
		}
		String value = (String) redisTemplate.opsForValue().getAndSet(key, toJson(obj));
		return parseJson(value, clazz);
	}

	/**
	 *     
	 * 
	 * @param key
	 * @return
	 */
	public static long generate(String key) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		return counter.incrementAndGet();
	}

	/**
	 * A    
	 * 
	 * @param key
	 * @param expireTime
	 * @return
	 */
	public static long generate(String key, Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		counter.expireAt(expireTime);
		return counter.incrementAndGet();
	}

	/**
	 *     
	 * 
	 * @param key
	 * @param increment
	 * @return
	 */
	public static long generate(String key, int increment) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		return counter.addAndGet(increment);
	}

	/**
	 *     
	 * 
	 * @param key
	 * @param increment
	 * @param expireTime
	 * @return
	 */
	public static long generate(String key, int increment, Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		counter.expireAt(expireTime);
		return counter.addAndGet(increment);
	}

	/**
	 *       JSONString
	 * 
	 * @param obj
	 * @return
	 */
	public static String toJson(Object obj) {
		return JSON.toJSONString(obj, SerializerFeature.SortField);
	}

	/**
	 *    JSON ObjectBean
	 * 
	 * @param obj
	 * @return
	 */
	public static  T parseJson(String json, Class clazz) {
		return JSON.parseObject(json, clazz);
	}

	/**
	 *    List  
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public static  List getList(String key, Class clazz) {
		try {
			Object cache = get(key);
			if (null != cache && StringUtils.isNotBlank(cache.toString())) {
				List cacheList = JSONArray.parseArray(JSONArray.parseArray(cache.toString()).toJSONString(), clazz);
				return cacheList;
			}
		} catch (Exception e) {
			log.error("redis    ,e:", e);
		}
		return null;
	}

	/**
	 *  List  JSONString    
	 * 
	 * @param key
	 * @param list
	 */
	public static  void setList(String key, List list) {
		if (StringUtils.isBlank(key) || CollectionUtils.isEmpty(list)) {
			return;
		}
		set(key, toJson(list));
	}

}