Redis как хранилище кэш данных
This commit is contained in:
@@ -104,6 +104,11 @@
|
||||
<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>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ks.server;
|
||||
|
||||
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.http.converter.HttpMessageConverter;
|
||||
@@ -7,9 +9,11 @@ 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;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ComponentScan({
|
||||
"ks.server.controllers",
|
||||
@@ -28,4 +32,16 @@ public class SpringConfigMVC implements WebMvcConfigurer {
|
||||
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
|
||||
configurer.favorPathExtension(false);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,49 @@
|
||||
package ks.server.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import ks.server.browser.Browser;
|
||||
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.stereotype.Service;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CinemaService {
|
||||
private static final Gson GSON = new Gson();
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
@Autowired
|
||||
private Jedis jedis;
|
||||
|
||||
public Animevost getCinema(String uri) {
|
||||
private Animevost realGet(String uri) {
|
||||
Animevost animevost = new Animevost();
|
||||
animevost.fillInfo(uri, applicationContext.getBean(Browser.class));
|
||||
return animevost;
|
||||
}
|
||||
|
||||
public Animevost getCinema(String uri) {
|
||||
if (jedis.isConnected()) {
|
||||
final String key = "cache:cinema:animevost:" + uri;
|
||||
if (jedis.exists(key)) {
|
||||
log.info("get data from redis");
|
||||
return GSON.fromJson(jedis.get(key), Animevost.class);
|
||||
} else {
|
||||
log.info("get data from site");
|
||||
Animevost animevost = realGet(uri);
|
||||
jedis.setex(key, 60, GSON.toJson(animevost));
|
||||
return animevost;
|
||||
}
|
||||
} else {
|
||||
jedis.connect();
|
||||
if (jedis.isConnected()) {
|
||||
return getCinema(uri);
|
||||
} else {
|
||||
log.warn("Redis disconnected!");
|
||||
return realGet(uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user