spring Cache
Spring对缓存提供了良好的支持,当然,都是基于AOP的,笔者只介绍两种最常用也最好用的方案
-
Redis缓存
Spring将Redis的Handler集成为注解,通过AOP可完美定位于任何位置
-
ConcurrentHashMap缓存
这种方案适合于少量数据的缓存,不难看出这个Map是置于堆中的,如果数据量过大很容易造成内存溢出
redis缓存代码
- 开启缓存
import org.springframework.cache.CacheManager;
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.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
import com.zgg.group2.scheduler.redis.config.RedisConfig;
@Configuration
@EnableCaching
public class CacheConfig {
/**
* redis cache
*/
@Bean
public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
@Bean
public static JedisConnectionFactory getConnectionFactory() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(RedisConfig.getMAXIDLE());
config.setMinIdle(RedisConfig.getMINIDLE());
config.setMaxTotal(RedisConfig.getMAXTOTAL());
config.setMaxWaitMillis(RedisConfig.getMAXWAITMILLIS());
RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration();
clusterConfig.addClusterNode(new RedisNode("192.168.89.221",7000));
clusterConfig.addClusterNode(new RedisNode("192.168.89.221",7001));
clusterConfig.addClusterNode(new RedisNode("192.168.89.221",7002));
clusterConfig.addClusterNode(new RedisNode("192.168.89.221",7003));
clusterConfig.addClusterNode(new RedisNode("192.168.89.221",7004));
clusterConfig.addClusterNode(new RedisNode("192.168.89.221",7005));
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(clusterConfig, config);
connectionFactory.setUsePool(true);
connectionFactory.afterPropertiesSet();
return connectionFactory;
}
@Bean
public static RedisTemplate<String, String> redisTemplate(JedisConnectionFactory rediscf) {
RedisTemplate<String, String> template = new RedisTemplate<String, String>();
template.setConnectionFactory(rediscf);
template.afterPropertiesSet();
return template;
}
}
- 在Service使用缓存
import java.util.List;
import org.springframework.cache.annotation.Cacheable;
import com.zgg.group2.common.page.PageParam;
import com.zgg.group2.common.page.PageResult;
import com.zgg.group2.rest.bean.ZgUser;
public interface RestService {
List<ZgUser> getZgUserList(ZgUser z);
ZgUser saveData(ZgUser z) ;
@Cacheable(value="keyCache", key="#zgUser.id")
PageResult<ZgUser> findPatentPageByParam(ZgUser zgUser, PageParam pageParam);
@Cacheable(value="keyCache", key="#zgUser.id")
PageResult<ZgUser> findPatentPageByParam(ZgUser zgUser, PageParam pageParam, String orderByStr);
@Cacheable(value="keyCache")
ZgUser getUserById(Integer id);
}
ConcurrentHashMap缓存
- 开启缓存
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CacheConfig {
/**
* process cache
*/
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
}
- 在Service使用缓存
import java.util.List;
import org.springframework.cache.annotation.Cacheable;
import com.zgg.group2.common.page.PageParam;
import com.zgg.group2.common.page.PageResult;
import com.zgg.group2.rest.bean.ZgUser;
public interface RestService {
List<ZgUser> getZgUserList(ZgUser z);
ZgUser saveData(ZgUser z) ;
@Cacheable(value="keyCache", key="#zgUser.id")
PageResult<ZgUser> findPatentPageByParam(ZgUser zgUser, PageParam pageParam);
@Cacheable(value="keyCache", key="#zgUser.id")
PageResult<ZgUser> findPatentPageByParam(ZgUser zgUser, PageParam pageParam, String orderByStr);
@Cacheable(value="keyCache")
ZgUser getUserById(Integer id);
}
项目中多使用这种简单易懂且能迅速上手的缓存对性能来说可能提高2倍以上的速度,能大大降低重复读取带来的应用和数据库的开销