Fix cache to use collections part 2. (#1373)

Closes #1369.
This commit is contained in:
Michael Reiche
2022-03-15 20:52:34 -07:00
committed by Michael Reiche
parent f80b997424
commit 01a211c443
2 changed files with 11 additions and 23 deletions

View File

@@ -105,7 +105,7 @@ public class DefaultCouchbaseCacheWriter implements CouchbaseCacheWriter {
@Override
public long clear(final String collectionName, final String pattern) {
QueryResult result = clientFactory.getScope().query(
"DELETE FROM `" + collectionName + "` where meta().id LIKE $pattern",
"DELETE FROM `" + getCollection(collectionName).name() + "` where meta().id LIKE $pattern",
queryOptions().scanConsistency(REQUEST_PLUS).metrics(true).parameters(JsonObject.create().put("pattern", pattern + "%")));
return result.metaData().metrics().map(QueryMetrics::mutationCount).orElse(0L);
}

View File

@@ -16,8 +16,6 @@
package org.springframework.data.couchbase.cache;
import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -57,7 +55,7 @@ class CouchbaseCacheIntegrationTests extends JavaIntegrationTests {
super.beforeEach();
cache = CouchbaseCacheManager.create(couchbaseTemplate.getCouchbaseClientFactory()).createCouchbaseCache("myCache",
CouchbaseCacheConfiguration.defaultCacheConfig());
clear(cache);
cache.clear();
ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
cacheManager = ac.getBean(CouchbaseCacheManager.class);
userRepository = ac.getBean(UserRepository.class);
@@ -66,17 +64,11 @@ class CouchbaseCacheIntegrationTests extends JavaIntegrationTests {
@AfterEach
@Override
public void afterEach() {
clear(cache);
cache.clear();
super.afterEach();
}
private void clear(CouchbaseCache c) {
couchbaseTemplate.getCouchbaseClientFactory().getCluster().query("SELECT count(*) from `" + bucketName() + "`",
QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS));
c.clear();
couchbaseTemplate.getCouchbaseClientFactory().getCluster().query("SELECT count(*) from `" + bucketName() + "`",
QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS));
}
@Test
void cachePutGet() {
@@ -109,17 +101,19 @@ class CouchbaseCacheIntegrationTests extends JavaIntegrationTests {
cache.put(user1.getId(), user1); // put user1
cache.put(user2.getId(), user2); // put user2
cache.evict(user1.getId()); // evict user1
assertNull(cache.get(user1.getId())); // get user1 -> not present
assertEquals(user2, cache.get(user2.getId()).get()); // get user2 -> present
}
@Test
void cacheHitMiss() {
void cacheClear() {
CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1");
CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2");
assertNull(cache.get(user2.getId())); // get user2 -> cacheMiss
cache.put(user1.getId(), null); // cache a null
assertNotNull(cache.get(user1.getId())); // cacheHit null
assertNull(cache.get(user1.getId()).get()); // fetch cached null
cache.put(user1.getId(), user1); // put user1
cache.put(user2.getId(), user2); // put user2
cache.clear();
assertNull(cache.get(user1.getId())); // get user1 -> not present
assertNull(cache.get(user2.getId())); // get user2 -> not present
}
@Test
@@ -131,12 +125,6 @@ class CouchbaseCacheIntegrationTests extends JavaIntegrationTests {
assertEquals(user1, cache.get(user1.getId()).get()); // user1.getId() is still user1
}
@Test // this test FAILS (local empty (i.e. fast) Couchbase installation)
public void clearFail() {
cache.put("KEY", "VALUE"); // no delay between put and clear, entry will not be
cache.clear(); // will not be indexed when clear() executes
assertNotNull(cache.get("KEY")); // will still find entry, clear failed to delete
}
@Test // this WORKS
public void clearWithDelayOk() throws InterruptedException {