标签:
<!-- redis cache related.....start --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> <!-- redis cache related.....end -->
/** * File Name:RedisCacheConfig.java * * Copyright Defonds Corporation 2015 * All Rights Reserved * */ package com.defonds.bdp.cache.redis; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; /** * * Project Name:bdp * Type Name:RedisCacheConfig * Type Description: * Author:Defonds * Create Date:2015-09-21 * * @version * */ @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); // Defaults redisConnectionFactory.setHostName("192.168.1.166"); redisConnectionFactory.setPort(6379); return redisConnectionFactory; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(cf); return redisTemplate; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // Number of seconds before expiration. Defaults to unlimited (0) cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds) return cacheManager; } }
<context:component-scan base-package="com.defonds.bdp.cache.redis" />
// R @Cacheable("provinceCities") public List<City> provinceCities(String province) { logger.debug("province=" + province); return this.cityMapper.provinceCities(province); } // R @Cacheable("searchCity") public City searchCity(String city_code){ logger.debug("city_code=" + city_code); return this.cityMapper.searchCity(city_code); }
// C @CacheEvict(value = { "provinceCities"}, allEntries = true) public void insertCity(String city_code, String city_jb, String province_code, String city_name, String city, String province) { City cityBean = new City(); cityBean.setCityCode(city_code); cityBean.setCityJb(city_jb); cityBean.setProvinceCode(province_code); cityBean.setCityName(city_name); cityBean.setCity(city); cityBean.setProvince(province); this.cityMapper.insertCity(cityBean); } // U @CacheEvict(value = { "provinceCities", "searchCity" }, allEntries = true) public int renameCity(String city_code, String city_name) { City city = new City(); city.setCityCode(city_code); city.setCityName(city_name); this.cityMapper.renameCity(city); return 1; } // D @CacheEvict(value = { "provinceCities", "searchCity" }, allEntries = true) public int deleteCity(String city_code) { this.cityMapper.deleteCity(city_code); return 1; }
@Cacheable("users") public User findByUsername(String username)
@Cacheable("users") public Integer getLoginCountByUsername(String username)
@Bean public KeyGenerator customKeyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for (Object obj : objects) { sb.append(obj.toString()); } return sb.toString(); } }; }
标签:
原文地址:http://www.cnblogs.com/dreammyle/p/5275007.html