Skip to content

Fix cache to use collections part 2. #1373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public boolean remove(final String collectionName, final String key) {
@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,7 +55,7 @@ public void beforeEach() {
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);
Expand All @@ -66,17 +64,11 @@ public void beforeEach() {
@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() {
Expand Down Expand Up @@ -109,17 +101,19 @@ void cacheEvict() {
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
Expand All @@ -131,12 +125,6 @@ void cachePutIfAbsent() {
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 {
Expand Down