[TOC]java
本文主要介紹使用spring-boot
集成jedis
的方式來操做redis。git
至於jedis
的單獨使用就沒必要多說了。github
此處的集成方式有兩種:web
手動配置集成jedisredis
使用spring-boot-starter-data-redis
集成spring
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--此處並非直接使用spring提供的redis-starter--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies>
application.yamlapp
jedis: host: 127.0.0.1 port: 6379 pool: max-idle: 300 min-idle: 10 max-total: 600 max-wait: 1000 block-when-exhausted: true
RedisConfig.javaide
package cn.hylexus.app.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisConfig { @Bean("jedis.config") public JedisPoolConfig jedisPoolConfig(// @Value("${jedis.pool.min-idle}") int minIdle, // @Value("${jedis.pool.max-idle}") int maxIdle, // @Value("${jedis.pool.max-wait}") int maxWaitMillis, // @Value("${jedis.pool.block-when-exhausted}") boolean blockWhenExhausted, // @Value("${jedis.pool.max-total}") int maxTotal) { JedisPoolConfig config = new JedisPoolConfig(); config.setMinIdle(minIdle); config.setMaxIdle(maxIdle); config.setMaxWaitMillis(maxWaitMillis); config.setMaxTotal(maxTotal); // 鏈接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true config.setBlockWhenExhausted(blockWhenExhausted); // 是否啓用pool的jmx管理功能, 默認true config.setJmxEnabled(true); return config; } @Bean public JedisPool jedisPool(// @Qualifier("jedis.config") JedisPoolConfig config, // @Value("${jedis.host}") String host, // @Value("${jedis.port}") int port) { return new JedisPool(config, host, port); } }
package cn.hylexus.app.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @Service public class RedisServiceImpl implements RedisService { // 此處直接注入便可 @Autowired private JedisPool jedisPool; @Override public String get(String key) { Jedis jedis = this.jedisPool.getResource(); String ret; try { ret = jedis.get(key); } finally { if (jedis != null) jedis.close(); } return ret; } @Override public boolean set(String key, String val) { Jedis jedis = this.jedisPool.getResource(); try { return "OK".equals(jedis.set(key, val)); } finally { if (jedis != null) jedis.close(); } } }
package cn.hylexus.app.service; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import cn.hylexus.app.SpringBootRedisJedispoolApplicationTests; public class RedisServiceImplTest extends SpringBootRedisJedispoolApplicationTests { @Autowired private RedisService redisService; @Test public void testGet() { // test set boolean status = this.redisService.set("foo", "bar"); Assert.assertTrue(status); // test get String str = this.redisService.get("foo"); Assert.assertEquals("bar", str); } }
https://github.com/hylexus/bl...spring-boot
<dependencies> <!--此處使用spring提供的針對於redis的starter--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
application.yaml測試
spring: redis: host: 127.0.0.1 port: 6379 password: null pool: max-idle: 300 min-idle: 10 max-active: 600 max-wait: 1000 timeout: 0
package cn.hylexus.app.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.SetOperations; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Autowired private RedisConnectionFactory redisConnectionFactory; /** * 實例化 RedisTemplate 對象 * */ @Bean public RedisTemplate<String, Object> functionDomainRedisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); this.initRedisTemplate(redisTemplate, redisConnectionFactory); return redisTemplate; } /** * 序列化設置 */ private void initRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setConnectionFactory(factory); } @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } @Bean public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); } }
package cn.hylexus.app; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; 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.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootRedisApplicationTests { @Autowired private ValueOperations<String, Object> valueOperations; @Autowired private RedisTemplate<String, Object> redisTemplate; @Test public void contextLoads() { } @Test public void testStringOps() { this.valueOperations.set("k1", "spring-redis"); Boolean hasKey = this.redisTemplate.hasKey("k1"); assertEquals(true, hasKey); Object str = this.valueOperations.get("k1"); assertNotNull(str); assertEquals("spring-redis", str.toString()); } }