paulwong

          Spring Boot使用redis做數(shù)據(jù)緩存

          1 添加redis支持

          在pom.xml中添加

          Xml代碼  收藏代碼
          1. <dependency>  
          2.           <groupId>org.springframework.boot</groupId>  
          3.           <artifactId>spring-boot-starter-redis</artifactId>  
          4.       </dependency>  

           

          2 redis配置

          Java代碼  收藏代碼
          1. @Configuration  
          2. @EnableCaching  
          3. public class RedisCacheConfig {  
          4.     @Bean  
          5.     public CacheManager cacheManager(  
          6.             @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {  
          7.         return new RedisCacheManager(redisTemplate);  
          8.     }  
          9.   
          10.     @Bean  
          11.     public RedisTemplate<String, String> redisTemplate(  
          12.             RedisConnectionFactory factory) {  
          13.         final StringRedisTemplate template = new StringRedisTemplate(factory);  
          14.         template.setValueSerializer(new Jackson2JsonRedisSerializer<SysUser>(  
          15.                 SysUser.class)); //請注意這里  
          16.   
          17.         return template;  
          18.     }  
          19. }  

           

          3 redis服務(wù)器配置

          Properties代碼  收藏代碼
          1. # REDIS (RedisProperties)  
          2. spring.redis.database= # database name  
          3. spring.redis.host=localhost # server host  
          4. spring.redis.password= # server password  
          5. spring.redis.port=6379 # connection port  
          6. spring.redis.pool.max-idle=8 # pool settings ...  
          7. spring.redis.pool.min-idle=0  
          8. spring.redis.pool.max-active=8  
          9. spring.redis.pool.max-wait=-1  
          10. spring.redis.sentinel.master= # name of Redis server  
          11. spring.redis.sentinel.nodes= # comma-separated list of host:port pairs  

           

          4 應(yīng)用

          Java代碼  收藏代碼
          1. /** 
          2. *此處的dao操作使用的是spring data jpa,使用@Cacheable可以在任意方法上,*比如@Service或者@Controller的方法上 
          3. */  
          4. public interface SysUserRepo1 extends CustomRepository<SysUser, Long> {  
          5.     @Cacheable(value = "usercache")  
          6.     public SysUser findByUsername(String username);  
          7. }  

           

          5 檢驗

          Java代碼  收藏代碼
          1. @Controller  
          2. public class TestController {  
          3.       
          4.       
          5.     @Autowired   
          6.     SysUserRepo1 sysUserRepo1;  
          7.     @RequestMapping("/test")  
          8.     public @ResponseBody String test(){  
          9.   
          10.         final SysUser loaded = sysUserRepo1.findByUsername("wyf");  
          11.         final SysUser cached = sysUserRepo1.findByUsername("wyf");  
          12.           
          13.         return "ok";  
          14.     }   
          15. }  

           

          效果如圖:



           

          posted on 2015-02-25 10:02 paulwong 閱讀(5283) 評論(3)  編輯  收藏 所屬分類: REDIS

          Feedback

          # re: Spring Boot使用redis做數(shù)據(jù)緩存[未登錄] 2015-05-14 15:31 zzf

          配置如何使用  回復(fù)  更多評論   

          # re: Spring Boot使用redis做數(shù)據(jù)緩存[未登錄] 2015-05-14 15:32 zzf

          REDIS服務(wù)器配置:Properties 如何使用  回復(fù)  更多評論   

          # re: Spring Boot使用redis做數(shù)據(jù)緩存 2015-05-19 13:11 paulwong

          @zzf
          這里只是摘錄了部份代碼,還有RedisConnectionFactory ,這個可以上網(wǎng)查些資料即可。  回復(fù)  更多評論   


          主站蜘蛛池模板: 东丽区| 偃师市| 大丰市| 达日县| 简阳市| 瑞安市| 高雄市| 子长县| 太谷县| 察隅县| 焦作市| 岗巴县| 灵寿县| 千阳县| 楚雄市| 本溪| 华池县| 藁城市| 黄平县| 福贡县| 鱼台县| 蒙山县| 枣庄市| 德州市| 大名县| 城固县| 万年县| 巴里| 丰台区| 鄂州市| 东乌珠穆沁旗| 乌鲁木齐县| 沧源| 华容县| 岐山县| 壶关县| 浦北县| 桐梓县| 托克逊县| 邻水| 大新县|