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