ssm中如何使用redis缓存
-
在SSM(Spring+SpringMVC+MyBatis)项目中使用Redis缓存可以提高系统的性能和响应速度。下面是在SSM中使用Redis缓存的步骤:
- 配置Redis依赖:
在pom.xml文件中添加Redis相关依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>- 配置Redis连接:
在application.properties文件中配置Redis连接信息:
spring.redis.host=127.0.0.1 spring.redis.port=6379- 创建Redis配置类:
创建一个RedisConfig类,并加上@Configuration注解,用于配置Redis连接池和RedisTemplate等信息:
@Configuration public class RedisConfig { @Bean public LettuceConnectionFactory lettuceConnectionFactory(Environment environment) { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName(environment.getProperty("spring.redis.host")); configuration.setPort(Integer.parseInt(environment.getProperty("spring.redis.port"))); return new LettuceConnectionFactory(configuration); } @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); // 设置序列化方式 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } }- 使用Redis缓存:
在需要使用缓存的方法上加上@Cacheable注解,指定缓存的名称和缓存的Key:
@Service public class UserService { @Autowired private RedisTemplate<String, Object> redisTemplate; @Cacheable(value = "userCache", key = "#userId") public User getUserById(Long userId) { // 从数据库中获取用户信息 User user = userDao.getUserById(userId); // 缓存用户信息到Redis redisTemplate.opsForValue().set("user:" + userId, user); return user; } }这样,当第一次调用getUserById方法时,会从数据库中获取用户信息并缓存到Redis中。当再次调用相同userId的getUserById方法时,会直接从Redis缓存中获取数据,不再访问数据库,提高系统的性能和响应速度。
以上就是在SSM中使用Redis缓存的步骤。通过配置Redis连接和使用@Cacheable注解,我们可以很方便地集成Redis缓存到SSM项目中。
2年前 - 配置Redis依赖:
-
使用Redis缓存可以大大提高系统的性能和响应速度。在SSM(Spring + SpringMVC + MyBatis)框架中,可以通过以下步骤来使用Redis缓存。
- 引入Redis依赖
首先,在项目的pom.xml文件中引入Redis的依赖。可以使用官方的Jedis客户端或者Lettuce客户端来与Redis进行交互。以下是Jedis和Lettuce的依赖配置示例:
<!-- Jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.7.0</version> </dependency> <!-- Lettuce --> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>6.0.1</version> </dependency>- 配置Redis连接信息
在Spring的配置文件(如applicationContext.xml)中添加Redis的连接信息。可以配置Redis的主机、端口、密码等。以下是一个Jedis的配置示例:
<!-- Jedis配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 设置最大连接数 --> <property name="maxTotal" value="100" /> <!-- 设置最大空闲连接数 --> <property name="maxIdle" value="50" /> <!-- 设置最小空闲连接数 --> <property name="minIdle" value="10" /> <!-- 设置连接超时时间(毫秒)--> <property name="maxWaitMillis" value="3000" /> <!-- 设置在获取连接时,是否进行有效性检查 --> <property name="testOnBorrow" value="true" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost" /> <property name="port" value="6379" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean>更多连接配置可以根据需要进行调整。
- 使用Redis缓存
在需要使用缓存的地方,可以通过以下方式使用Redis缓存。
- 使用Spring的
@Cacheable注解。在需要缓存的方法上添加该注解,并指定缓存的名称以及缓存的key。如下例所示:
@Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override @Cacheable(value = "users", key = "'user:' + #id") public User getUserById(int id) { return userDao.getUserById(id); } }在上述示例中,
value指定了缓存的名称为"users",key指定了缓存的key为"user:"加上方法的参数id。当调用getUserById方法时,会先从Redis缓存中查找对应的数据,如果缓存中不存在,则执行方法体,并将方法返回的结果缓存到Redis中。- 使用RedisTemplate进行缓存操作。可以通过RedisTemplate来直接操作Redis缓存,例如:
@Autowired private RedisTemplate<String, Object> redisTemplate; public void saveUser(User user) { redisTemplate.opsForValue().set("user:" + user.getId(), user); } public User getUser(int id) { return (User) redisTemplate.opsForValue().get("user:" + id); }上述示例中,通过
redisTemplate.opsForValue().set方法将'("user:" + user.getId(), user)存储到Redis中,通过redisTemplate.opsForValue().get`方法获取缓存中的数据。- 配置缓存过期时间
可以通过为缓存添加过期时间,使缓存自动失效。可以通过以下方式配置过期时间:
- 在使用
@Cacheable注解时,通过expire属性指定过期时间。例如,在上述的@Cacheable注解中添加expire = 600表示缓存10分钟后自动失效。 - 在RedisTemplate操作缓存时,通过调用
redisTemplate.expire方法设置缓存的过期时间。例如:
redisTemplate.expire("user:" + id, 600, TimeUnit.SECONDS);上述示例中,将id为
id的用户缓存设置为10分钟后自动失效。- 清除缓存
当数据发生改变时,需要清除相应的缓存。可以通过以下方式清除缓存:
- 在使用
@CachePut注解时,该注解会在方法执行完毕后将结果保存到缓存中,所以需要在修改数据的方法上添加@CachePut注解,以更新缓存。例如:
@Override @CachePut(value = "users", key = "'user:' + #user.getId()") public User updateUser(User user) { userDao.updateUser(user); return user; }在上述示例中,先更新数据库中的数据,然后再将更新后的结果保存到缓存中。
- 使用RedisTemplate操作缓存时,可以通过调用
redisTemplate.delete方法来删除缓存。例如:
redisTemplate.delete("user:" + id);上述示例中,将id为
id的用户缓存从Redis中删除。通过以上步骤,就可以在SSM框架中使用Redis缓存来提高系统的性能和响应速度了。需要根据具体的业务需求和性能要求对缓存进行合理的配置和管理。
2年前 - 引入Redis依赖
-
SSM(Spring + SpringMVC + MyBatis)是一种常见的Java开发框架,Redis是一种高性能的Key-Value存储系统,它可以用作缓存数据库。在SSM中使用Redis缓存可以提高系统的性能和响应速度。
下面是在SSM中使用Redis缓存的具体操作流程。
-
下载和安装Redis:可以从Redis官网下载最新版本的Redis,并按照官方文档进行安装和配置。
-
添加Redis依赖:在项目的pom.xml文件中添加Redis的依赖。
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency>- 配置Redis连接参数:在Spring的配置文件中,添加Redis连接参数的配置。
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="100"/> <property name="maxIdle" value="10"/> <property name="maxWaitMillis" value="10000"/> <property name="testOnBorrow" value="true"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="hostName" value="localhost"/> <property name="port" value="6379"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="valueSerializer" ref="stringRedisSerializer"/> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>- 使用Redis缓存:在需要使用缓存的方法上添加缓存相关的注解。
在Service层的方法中,可以使用
@Cacheable注解来指定缓存的Key和缓存的条件。@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Cacheable(key = "'user:' + #id", unless="#result == null") public User getUserById(int id) { return userMapper.selectByPrimaryKey(id); } }- 清除缓存:在需要清除缓存的方法上添加
@CacheEvict注解。
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @CacheEvict(key = "'user:' + #id") public void deleteUserById(int id) { userMapper.deleteByPrimaryKey(id); } }需要注意的是,为了能够使用Redis缓存,需要确保配置好Redis的连接参数,并在需要使用缓存的方法上添加正确的注解。此外,为了确保数据的一致性,还需要在更新、删除等操作之后清除缓存。
另外,Redis还支持其他更高级的缓存操作,比如设置缓存的过期时间、使用分布式锁等,可以根据具体的业务需求来进行配置和使用。
2年前 -