Redis与Spring Boot JDBC Template集成

huangapple go评论78阅读模式
英文:

Redis integration with spring boot JDBC Template

问题

我目前正在开发一个CRUD应用程序,旨在整合像AWS、MySQL、Docker等行业标准工具。为了与数据库连接,我正在使用Spring的传统Spring JDBC模板。然而,我注意到Spring的许多工具主要是为Spring JPA构建的。

我想深入了解低级细节,并将Redis用作内存数据库进行缓存。然而,我找到的大多数教程都集中在如何在Spring JPA中使用Redis上。是否有人能够提供一个关于如何将Redis与Spring的JDBC模板集成的资源或指南?我将非常感激任何帮助。谢谢!

我已经搜索了很多资源,但它们似乎都在指向使用Spring JPA。

英文:

I'm currently working on a CRUD application and aiming to incorporate industry-standard tools like AWS, MySQL, Docker, and more. For connecting with the database, I'm using Spring's plain old Spring JDBC Templates. However, I've noticed that many of Spring's tools are primarily built for Spring JPA.

I'd like to dive into the low-level details and utilize Redis as an in-memory database for caching. However, most tutorials I've come across focus on using Redis with Spring JPA. Could someone please provide me with a resource or guide on how to integrate Redis with Spring's JDBC Templates? I would greatly appreciate any assistance. Thank you!

I have googled a lot of resources and all of them seem to be pointing to use spring JPA.

答案1

得分: 1

Redis可以非常容易地集成到Spring Boot中,而无需依赖Spring Data Redis。

使用依赖项:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

在应用程序中创建一个RedisClient/Helper的bean:

@Component
@Slf4j
public class RedisClient {

    private JedisPool pool;

    @PostConstruct
    public void initialize() {
        try {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMinIdle(ApplicationConstant.REDIS_MIN_IDLE);
            poolConfig.setMaxTotal(ApplicationConstant.REDIS_MAX_TOTAL);
            poolConfig.setMaxIdle(ApplicationConstant.REDIS_MAX_IDLE);
            URI uri = URI.create(ApplicationConstant.REDIS_SERVER_URL);
            pool = new JedisPool(poolConfig, uri);
            try (Jedis client = pool.getResource()){
                if (client.isBroken()) throw new JedisConnectionException("无法创建Jedis池,请检查配置。");
            }
            log.info("Redis连接已建立。");
        } catch (JedisConnectionException e) {
            log.error("创建Jedis池时出错", e);
            throw e;
        }
    }

    @PreDestroy
    public void close() {
        if (pool != null && !pool.isClosed()) {
            pool.close();
        }
    }

    public String get(String key) {
        try(Jedis jedis = pool.getResource()) {
            return jedis.get(key);
        } 
    }

    public String set(String key, String value) {
        try(Jedis jedis = pool.getResource()) {
            return jedis.set(key, value);
        }
    }
}

现在只需在需要的地方进行自动装配/注入bean。

@Repository
public class MyDao{

    @Autowired
    private EntityManager em;
    @Autowired
    private RedisClient redisClient;

    public String getData(String key) {
        String obj = redisClient.get(key);
        if (obj != null) {
           return obj;
        }
        String result = em.find(String.class, key);
        redisClient.set(key, result);
        return result;
    }
}

参考链接:https://redis.io/docs/clients/java/https://www.baeldung.com/jedis-java-redis-client-library

英文:

Redis can be integrated into spring boot very easily without Spring Data Redis dependency.

Use dependency:

&lt;dependency&gt;
&lt;groupId&gt;redis.clients&lt;/groupId&gt;
&lt;artifactId&gt;jedis&lt;/artifactId&gt;
&lt;/dependency&gt;

In Application create a bean of RedisClient/Helper

@Component
@Slf4j
public class RedisClient {
private JedisPool pool;
@PostConstruct
public void initialize() {
try {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMinIdle(ApplicationConstant.REDIS_MIN_IDLE);
poolConfig.setMaxTotal(ApplicationConstant.REDIS_MAX_TOTAL);
poolConfig.setMaxIdle(ApplicationConstant.REDIS_MAX_IDLE);
URI uri = URI.create(ApplicationConstant.REDIS_SERVER_URL);
pool = new JedisPool(poolConfig, uri);
try (Jedis client = pool.getResource()){
if (client.isBroken()) throw new JedisConnectionException(&quot;Unable to create Jedis pool please check the configurations.&quot;);
}
log.info(&quot;Redis connection established.&quot;);
} catch (JedisConnectionException e) {
log.error(&quot;Error in creating jedis pool&quot;, e);
throw e;
}
}
@PreDestroy
public void close() {
if (pool!=null &amp;&amp; !pool.isClosed()) {
pool.close();
}
}
public String get(String key) {
try(Jedis jedis = pool.getResource()) {
return jedis.get(key);
} 
}
public String set(String key, String value) {
try(Jedis jedis = pool.getResource()) {
return jedis.set(key, value);
}
}
}

Now simply Autowire/inject the bean wherever you want.

@Repository
public class MyDao{
@Autowired
private EntityManager em;
@Autowired
private RedisClient redisClient;
public String getData(String key) {
String obj = redisClient.get(key);
if (obj != null) {
return obj;
}
String result= em.find(String.class, key);
redisClient.set(key, result);
return resp;
}
}

Reference- https://redis.io/docs/clients/java/,
https://www.baeldung.com/jedis-java-redis-client-library

huangapple
  • 本文由 发表于 2023年7月3日 18:52:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定