gpt4 book ai didi

java - 使用 JpaRepository 缓存

转载 作者:行者123 更新时间:2023-12-04 23:49:13 28 4
gpt4 key购买 nike

嗨,我已经通过以下方式扩展了 JpaRepository 接口(interface)。

public interface StudentRepository extends JpaRepository<Student,Integer>
{
@Query(value= "SELECT s.id FROM student as s where s.createdat > ADDDATE(CURRENT_DATE, :maxage ", nativeQuery = true )
public List<Integer> findWaitingStudentIds(@Param("maxage")int maxAge);
}

这里是 Entity类(class)。
@Entity(name="student ")
public class Student implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private Integer id;
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false,insertable = false)
private Date createdat;
}

我想为“List findWaitingStudentIds”方法添加缓存。我怎样才能做到这一点?

最佳答案

我可以从这个 StackOverflow 问题中复制粘贴我的答案:
How should I implement a cache object/system in Spring?

Spring introduced abstraction for Cache in 3.x RELEASE. You can readabout it in official Spring documentation (the site is down today forsome reason :)), or at this post for example.

http://dzone.com/articles/spring-cache-abstraction-0

With this abstraction, all you need to do to enable cache is to addsome annotations to your services, like

To add value to the cache

@Cacheable("customers")
public Customer findCustomer(long customerId) {...}

To remove value to the cache

@CacheEvict(value="customer", allEntries = true)
public void removeAllCustomers(long customerId) {...}

关于java - 使用 JpaRepository 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26837789/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com