1、引入 spring-boot-starter-redis
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、application.yml配置redis信息
spring:
redis:
host: 127.0.0.1
port: 6379
password:
pool:
max-active: 100
max-idle: 10
max-wait: 100000
timeout: 0
3、集成Redis
基于JedisPool配置,使用RedisTemplate来操作redis的方式。
a、RedisConfig.java
package com.lynch.redis; import java.lang.reflect.Method; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration @EnableAutoConfiguration public class RedisConfig { /** * 获取JedisPoolConfig配置 * * @return */ @Bean @ConfigurationProperties(prefix = "spring.redis.pool") public JedisPoolConfig getRedisConfig(){ JedisPoolConfig config = new JedisPoolConfig(); return config; } /** * 获取JedisConnectionFactory工厂 * * @return */ @Bean @ConfigurationProperties(prefix = "spring.redis") public JedisConnectionFactory getConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setUsePool(true); JedisPoolConfig config = getRedisConfig(); factory.setPoolConfig(config); return factory; } /** * 获取RedisTemplate模板 * * @return */ @Bean public RedisTemplate<?, ?> getRedisTemplate() { JedisConnectionFactory factory = getConnectionFactory(); RedisTemplate<?, ?> template = new StringRedisTemplate(factory); return template; } }
@Configuration注解 用于定义配置类,可替换xml配置文件,[email protected]法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
@EnableAutoConfiguration注解
启用Spring应用程序上下文的自动配置,尝试猜测和配置您可能需要的bean。自动配置类通常基于类路径和定义的bean应用。
@ConfigurationProperties注解
用于读取配置文件的信息,在这里是读取配置在yml里的redis的相关配置项。
@Bean注解
用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean
b、RedisService.java
package com.lynch.redis; public interface RedisService { /** * set存数据 * @param key * @param value * @return */ boolean set(String key, String value); /** * get获取数据 * @param key * @return */ String get(String key); /** * 设置有效天数 * @param key * @param expire * @return */ boolean expire(String key, long expire); /** * 移除数据 * @param key * @return */ boolean remove(String key); }
c、RedisServiceImpl.java
package com.lynch.redis; import java.util.concurrent.TimeUnit; import javax.annotation.Resource; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Service; @Service public class RedisServiceImpl implements RedisService { @Resource private RedisTemplate<String, ?> redisTemplate; @Override public boolean set(final String key, final String value) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); connection.set(serializer.serialize(key), serializer.serialize(value)); return true; } }); return result; } @Override public String get(final String key) { String result = redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); byte[] value = connection.get(serializer.serialize(key)); return serializer.deserialize(value); } }); return result; } @Override public boolean expire(final String key, long expire) { return redisTemplate.expire(key, expire, TimeUnit.SECONDS); } @Override public boolean remove(final String key) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.del(key.getBytes()); return true; } }); return result; } }
4、Redis测试
package com.lynch.config; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.lynch.redis.RedisService; @RunWith(SpringRunner.class) @SpringBootTest public class RedisServiceTest { @Autowired private RedisService redisService; @Test public void contextLoads() { } @Test public void setString() { redisService.set("redis_string_test", "springboot redis!中国"); } @Test public void getString() { String result = redisService.get("redis_string_test"); System.out.println(result); } @Test public void remove() { System.out.println(redisService.remove("redis_string_test")); } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/16116.html