Redis как хранилище кэш данных
This commit is contained in:
@@ -104,6 +104,11 @@
|
|||||||
<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>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ks.server;
|
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.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
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.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;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan({
|
@ComponentScan({
|
||||||
"ks.server.controllers",
|
"ks.server.controllers",
|
||||||
@@ -28,4 +32,16 @@ public class SpringConfigMVC implements WebMvcConfigurer {
|
|||||||
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
|
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
|
||||||
configurer.favorPathExtension(false);
|
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;
|
package ks.server.service;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import ks.server.browser.Browser;
|
import ks.server.browser.Browser;
|
||||||
import ks.server.cinema.Animevost;
|
import ks.server.cinema.Animevost;
|
||||||
|
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.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class CinemaService {
|
public class CinemaService {
|
||||||
|
private static final Gson GSON = new Gson();
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
|
@Autowired
|
||||||
|
private Jedis jedis;
|
||||||
|
|
||||||
public Animevost getCinema(String uri) {
|
private Animevost realGet(String uri) {
|
||||||
Animevost animevost = new Animevost();
|
Animevost animevost = new Animevost();
|
||||||
animevost.fillInfo(uri, applicationContext.getBean(Browser.class));
|
animevost.fillInfo(uri, applicationContext.getBean(Browser.class));
|
||||||
return animevost;
|
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