Переходим на Spring Data Redis
This commit is contained in:
@@ -64,6 +64,18 @@
|
|||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SPRING DATA -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-redis</artifactId>
|
||||||
|
<version>2.1.3.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>redis.clients</groupId>
|
||||||
|
<artifactId>jedis</artifactId>
|
||||||
|
<version>2.9.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- JETTY -->
|
<!-- JETTY -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
@@ -104,11 +116,6 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.8.5</version>
|
<version>2.8.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>redis.clients</groupId>
|
|
||||||
<artifactId>jedis</artifactId>
|
|
||||||
<version>3.0.1</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||||
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.converter.json.GsonHttpMessageConverter;
|
import org.springframework.http.converter.json.GsonHttpMessageConverter;
|
||||||
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
|
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import redis.clients.jedis.Jedis;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -34,14 +36,12 @@ public class SpringConfigMVC implements WebMvcConfigurer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Jedis jedis() {
|
public JedisConnectionFactory redisConnectionFactory() {
|
||||||
Jedis jedis = new Jedis("127.0.0.1", 6379);
|
return new JedisConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379));
|
||||||
jedis.connect();
|
|
||||||
if (jedis.isConnected()) {
|
|
||||||
log.info("Redis server ready");
|
|
||||||
} else {
|
|
||||||
log.warn("Redis server not ready!");
|
|
||||||
}
|
}
|
||||||
return jedis;
|
|
||||||
|
@Bean
|
||||||
|
public StringRedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
|
||||||
|
return new StringRedisTemplate(jedisConnectionFactory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,11 @@ import ks.server.cinema.Animevost;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.data.redis.RedisConnectionFailureException;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import redis.clients.jedis.Jedis;
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@@ -16,7 +19,7 @@ public class CinemaService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
@Autowired
|
@Autowired
|
||||||
private Jedis jedis;
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
private Animevost realGet(String uri) {
|
private Animevost realGet(String uri) {
|
||||||
Animevost animevost = new Animevost();
|
Animevost animevost = new Animevost();
|
||||||
@@ -25,25 +28,24 @@ public class CinemaService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Animevost getCinema(String uri) {
|
public Animevost getCinema(String uri) {
|
||||||
if (jedis.isConnected()) {
|
|
||||||
final String key = "cache:cinema:animevost:" + uri;
|
final String key = "cache:cinema:animevost:" + uri;
|
||||||
if (jedis.exists(key)) {
|
|
||||||
|
try {
|
||||||
|
Boolean hasKey = redisTemplate.hasKey(key);
|
||||||
|
|
||||||
|
if (hasKey != null && hasKey) {
|
||||||
log.info("get data from redis");
|
log.info("get data from redis");
|
||||||
return GSON.fromJson(jedis.get(key), Animevost.class);
|
return GSON.fromJson(redisTemplate.opsForValue().get(key), Animevost.class);
|
||||||
} else {
|
} else {
|
||||||
log.info("get data from site");
|
log.info("get data from site");
|
||||||
Animevost animevost = realGet(uri);
|
Animevost animevost = realGet(uri);
|
||||||
jedis.setex(key, 60, GSON.toJson(animevost));
|
redisTemplate.opsForValue().set(key, GSON.toJson(animevost), 60, TimeUnit.SECONDS);
|
||||||
return animevost;
|
return animevost;
|
||||||
}
|
}
|
||||||
} else {
|
} catch (RedisConnectionFailureException e) {
|
||||||
jedis.connect();
|
log.warn("Redis connection failure!");
|
||||||
if (jedis.isConnected()) {
|
log.debug("", e);
|
||||||
return getCinema(uri);
|
|
||||||
} else {
|
|
||||||
log.warn("Redis disconnected!");
|
|
||||||
return realGet(uri);
|
return realGet(uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user