javascript
使用Spring Data Cassandra缓存的预备语句
今天,我有一篇簡(jiǎn)短的文章,內(nèi)容涉及在Spring Data Cassandra中使用Prepared Statements。 Spring為您提供了一些實(shí)用程序,使您可以更輕松地使用“預(yù)備語(yǔ)句”,而不必依靠自己使用Datastax Java驅(qū)動(dòng)程序手動(dòng)注冊(cè)查詢。 Spring代碼提供了一個(gè)緩存來(lái)存儲(chǔ)經(jīng)常使用的準(zhǔn)備好的語(yǔ)句。 允許您通過(guò)緩存執(zhí)行查詢,緩存可以從緩存中檢索準(zhǔn)備好的查詢,也可以在執(zhí)行之前添加一個(gè)新查詢。
為了簡(jiǎn)短起見(jiàn),我們可能應(yīng)該開(kāi)始看一些代碼。
依存關(guān)系
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version> </parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-cassandra</artifactId></dependency> </dependencies>使用Spring Boot 2.0.5.RELEASE將拉入Spring Data Cassandra的2.0.10.RELEASE 。
使用準(zhǔn)備好的語(yǔ)句
讓我們直接進(jìn)入:
@Repository public class PersonRepository extends SimpleCassandraRepository<Person, PersonKey> {private final Session session;private final CassandraOperations cassandraTemplate;private final PreparedStatementCache cache = PreparedStatementCache.create();public PersonRepository(Session session,CassandraEntityInformation entityInformation,CassandraOperations cassandraTemplate) {super(entityInformation, cassandraTemplate);this.session = session;this.cassandraTemplate = cassandraTemplate;}// using ORMpublic List<Person> findByFirstNameAndDateOfBirth(String firstName, LocalDate dateOfBirth) {return cassandraTemplate.getCqlOperations().query(findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),(row, rowNum) -> cassandraTemplate.getConverter().read(Person.class, row));}private BoundStatement findByFirstNameAndDateOfBirthQuery(String firstName, LocalDate dateOfBirth) {return CachedPreparedStatementCreator.of(cache,select().all().from("people_by_first_name").where(eq("first_name", bindMarker("first_name"))).and(eq("date_of_birth", bindMarker("date_of_birth")))).createPreparedStatement(session).bind().setString("first_name", firstName).setDate("date_of_birth", toCqlDate(dateOfBirth));}private com.datastax.driver.core.LocalDate toCqlDate(LocalDate date) {return com.datastax.driver.core.LocalDate.fromYearMonthDay(date.getYear(), date.getMonth().getValue(), date.getDayOfMonth());}// without ORMpublic List<Person> findByFirstNameAndDateOfBirthWithoutORM(String firstName, LocalDate dateOfBirth) {return cassandraTemplate.getCqlOperations().query(findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),(row, rowNum) -> convert(row));}private Person convert(Row row) {return new Person(new PersonKey(row.getString("first_name"),toLocalDate(row.getDate("date_of_birth")),row.getUUID("person_id")),row.getString("last_name"),row.getDouble("salary"));}private LocalDate toLocalDate(com.datastax.driver.core.LocalDate date) {return LocalDate.of(date.getYear(), date.getMonth(), date.getDay());} }這里有相當(dāng)數(shù)量的樣板代碼,因此我們可以訪問(wèn)Spring Data的ORM。 我還提供了代碼來(lái)演示如何在不使用ORM的情況下實(shí)現(xiàn)相同的目標(biāo)(無(wú)論如何,直接將查詢直接映射到對(duì)象)。
讓我們更仔細(xì)地研究一種方法:
public List<Person> findByFirstNameAndDateOfBirth(String firstName, LocalDate dateOfBirth) {return cassandraTemplate.getCqlOperations().query(findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),(row, rowNum) -> cassandraTemplate.getConverter().read(Person.class, row)); }private BoundStatement findByFirstNameAndDateOfBirthQuery(String firstName, LocalDate dateOfBirth) {return CachedPreparedStatementCreator.of(cache,select().all().from("people_by_first_name").where(eq("first_name", bindMarker("first_name"))).and(eq("date_of_birth", bindMarker("date_of_birth")))).createPreparedStatement(session).bind().setString("first_name", firstName).setDate("date_of_birth", toCqlDate(dateOfBirth)); }CachedPreparedStatementCreator完全按照其說(shuō)的進(jìn)行操作...它創(chuàng)建緩存的Prepared Statements。 of方法采用實(shí)例化Bean時(shí)定義的cache ,并創(chuàng)建第二個(gè)參數(shù)定義的新查詢。 如果查詢是最近已經(jīng)注冊(cè)的查詢,即它已經(jīng)在緩存中。 然后,從那里開(kāi)始查詢,而不是完成注冊(cè)新語(yǔ)句的整個(gè)過(guò)程。
傳入的查詢是一個(gè)RegularStatement ,可以通過(guò)調(diào)用createPreparedStatement將它轉(zhuǎn)換為PreparedStatement (我猜是吧)。 現(xiàn)在,我們可以將值綁定到查詢,因此它實(shí)際上可以做一些有用的事情。
就緩存Prepared Statements而言,這就是您要做的全部。 還有其他方法可以執(zhí)行此操作,例如,您可以手動(dòng)使用PreparedStatementCache或定義自己的緩存實(shí)現(xiàn)。 無(wú)論您的船浮在水上。
您現(xiàn)在已經(jīng)到了這篇簡(jiǎn)短文章的結(jié)尾,希望它實(shí)際上包含了足夠有用的信息……
在本文中,我們介紹了如何使用CachedPreparedStatementCreator創(chuàng)建和將Prepared Statements放入高速緩存中,以便在以后更快地執(zhí)行。 使用Spring Data提供的類,我們可以減少需要編寫的代碼量。
這篇文章中使用的代碼可以在我的GitHub上找到 。
如果您認(rèn)為這篇文章有幫助,可以在Twitter上@LankyDanDev關(guān)注我,以跟上我的新文章。
翻譯自: https://www.javacodegeeks.com/2018/10/cached-prepared-statements-cassandra.html
總結(jié)
以上是生活随笔為你收集整理的使用Spring Data Cassandra缓存的预备语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 防疫备案表(抗疫备案)
- 下一篇: ddoS是什么意思(ddos事件标志着)