Redis 缓存 + Spring 的集成示例

整合 spring 4(包括mvc、context、orm) + mybatis 3 示例》一文简要介绍了最新版本的 Spring MVC、IOC、MyBatis ORM 三者的整合以及声明式事务处理。现在我们需要把缓存也整合进来,缓存我们选用的是 Redis,本文将在该文示例基础上介绍 Redis 缓存 + Spring 的集成。关于 Redis 服务器的搭建请参考博客《 Redhat5.8 环境下编译安装 Redis 并将其注册为系统服务》。

1. 依赖包安装

pom.xml 加入:
		<!-- 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 -->

2. Spring 项目集成进缓存支持

要启用缓存支持,我们需要创建一个新的 CacheManager bean。 CacheManager 接口有很多实现,本文演示的是和 Redis 的集成,自然就是用 RedisCacheManager 了。Redis 不是应用的共享内存,它只是一个内存服务器,就像 MySql 似的,我们需要将应用连接到它并使用某种“语言”进行交互,因此我们还需要一个连接工厂以及一个 Spring 和 Redis 对话要用的 RedisTemplate,这些都是 Redis 缓存所必需的配置,把它们都放在自定义的 CachingConfigurerSupport 中:
/**
 * 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 
  • 46
    点赞
  • 256
    收藏
    觉得还不错? 一键收藏
  • 70
    评论
好的,以下是Java中使用注解操作Redis示例: 首先,我们需要在pom.xml文件中添加Redis的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来,我们需要在Spring Boot的Application类上添加@EnableCaching注解来开启缓存: ```java @SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 然后,我们需要在Redis的配置文件application.properties中添加Redis的连接信息: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 接下来,我们就可以使用注解来操作Redis了。下面是一个使用注解操作Redis缓存示例: ```java @Service public class UserService { @Autowired private UserRepository userRepository; @Cacheable(value = "userCache", key = "#id") public User getUserById(Long id) { System.out.println("从数据库中获取用户信息"); return userRepository.findById(id).orElse(null); } @CachePut(value = "userCache", key = "#user.id") public User saveUser(User user) { System.out.println("保存用户信息到数据库"); return userRepository.save(user); } @CacheEvict(value = "userCache", key = "#id") public void deleteUserById(Long id) { System.out.println("从数据库中删除用户信息"); userRepository.deleteById(id); } } ``` 上面的示例中,我们使用了三个注解来操作Redis缓存: - @Cacheable:表示方法的结果可以被缓存,如果缓存中有数据,则直接返回缓存数据,否则执行方法并将结果放入缓存中。 - @CachePut:表示方法的结果需要被缓存,每次都会执行方法,并将结果放入缓存中。 - @CacheEvict:表示方法会从缓存中删除数据。 在这个示例中,我们使用了value属性来指定缓存的名称,key属性来指定缓存的键,#id和#user.id是SpEL表达式,用于获取方法参数中的值。 以上就是一个使用注解操作Redis缓存示例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 70
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值