日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

cassandra使用心得_使用Spring Data Cassandra缓存的预备语句

發(fā)布時間:2023/12/3 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cassandra使用心得_使用Spring Data Cassandra缓存的预备语句 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

cassandra使用心得

今天,我有一篇簡短的文章,內(nèi)容涉及在Spring Data Cassandra中使用Prepared Statements。 Spring為您提供了一些實用程序,使您可以更輕松地使用“預(yù)備語句”,而不是依靠自己使用Datastax Java驅(qū)動程序手動注冊查詢。 Spring代碼提供了一個緩存來存儲經(jīng)常使用的準(zhǔn)備好的語句。 允許您通過緩存執(zhí)行查詢,緩存可以從緩存中檢索準(zhǔn)備好的查詢,也可以在執(zhí)行之前添加一個新查詢。

為了簡短起見,我們可能應(yīng)該開始看一些代碼。

依存關(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)備好的語句

讓我們直接進(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ù)量的樣板代碼,因此我們可以訪問Spring Data的ORM。 我還提供了代碼來演示如何在不使用ORM的情況下實現(xiàn)相同的目標(biāo)(無論如何,它還是直接從查詢直接映射到對象)。

讓我們更仔細(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完全按照其說的進(jìn)行操作…它創(chuàng)建緩存的Prepared Statements。 of方法采用實例化Bean時定義的cache ,并創(chuàng)建第二個參數(shù)定義的新查詢。 如果查詢是最近已經(jīng)注冊的查詢,即它已經(jīng)在緩存中。 然后,從那里開始查詢,而不是完成注冊新語句的整個過程。

傳入的查詢是一個RegularStatement ,可以通過調(diào)用createPreparedStatement將它轉(zhuǎn)換為PreparedStatement (我猜是吧)。 現(xiàn)在,我們可以將值綁定到查詢,因此它實際上可以做一些有用的事情。

就緩存Prepared Statements而言,這就是您要做的全部。 還有其他方法可以執(zhí)行此操作,例如,您可以手動使用PreparedStatementCache或定義自己的緩存實現(xiàn)。 無論您的船浮在水上。

您現(xiàn)在已經(jīng)到了這篇簡短文章的結(jié)尾,希望它實際上包含了足夠有用的信息……

在本文中,我們介紹了如何使用CachedPreparedStatementCreator創(chuàng)建和將Prepared Statements放入高速緩存中,以便在以后更快地執(zhí)行。 使用Spring Data提供的類,我們可以減少需要編寫的代碼量。

這篇文章中使用的代碼可以在我的GitHub上找到 。

如果您發(fā)現(xiàn)此帖子有幫助,可以在Twitter上@LankyDanDev關(guān)注我,以了解我的新帖子。

翻譯自: https://www.javacodegeeks.com/2018/10/cached-prepared-statements-cassandra.html

cassandra使用心得

總結(jié)

以上是生活随笔為你收集整理的cassandra使用心得_使用Spring Data Cassandra缓存的预备语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。