- structure
- build.gradle
group 'org.blog.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile group: 'org.springframework.data', name: 'spring-data-redis', version: '1.7.2.RELEASE'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
}
- application.yml
server: port: 8080 redis: host: name: 192.168.122.1 port: 6379
- RedisApplication.java
package org.blog.test;
import lombok.extern.slf4j.Slf4j;
import org.blog.test.cache.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Slf4j
public class RedisApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
@Autowired
private CacheService cacheService;
@Override
public void run(String... args) throws Exception {
log.info("start");
cacheService.putValue("test","test value");
}
}
- RedisConfig.java
package org.blog.test.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
@Value("${redis.host.name}")
private String redisHostName;
@Value("${redis.port}")
private Integer redisPort;
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMaxTotal(30);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
jedisConnectionFactory.setHostName(redisHostName);
jedisConnectionFactory.setPort(redisPort);
jedisConnectionFactory.setUsePool(true);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate redisTemplate() {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
}
- CacheService.java
package org.blog.test.cache.service;
public interface CacheService {
void putValue(String key, String value);
}
- RedisServiceImpl.java
package org.blog.test.cache.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.blog.test.cache.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class RedisServiceImpl implements CacheService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public void putValue(String key, String value) {
redisTemplate.boundValueOps(key).set(value);
log.info("add cache key : [{}], value: [{}]", key, value);
}
}
- result
You can find the key, value in redis server.
[root@localhost redis-3.2.3]# redis-cli 127.0.0.1:6379> keys * 1) "\xac\xed\x00\x05t\x00\x04test" 127.0.0.1:6379> get "\xac\xed\x00\x05t\x00\x04test" "\xac\xed\x00\x05t\x00\ntest value"

댓글 없음 :
댓글 쓰기