Skip to content

Fix cache.clear() to use collection. #1371

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 1 commit into from
Mar 16, 2022
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 @@ -138,12 +138,12 @@ public boolean evictIfPresent(final Object key) {

@Override
public boolean invalidate() {
return cacheWriter.clear(cacheConfig.getKeyPrefixFor(name)) > 0;
return cacheWriter.clear(cacheConfig.getCollectionName(), cacheConfig.getKeyPrefixFor(name)) > 0;
}

@Override
public void clear() {
cacheWriter.clear(cacheConfig.getKeyPrefixFor(name));
cacheWriter.clear( cacheConfig.getCollectionName(), cacheConfig.getKeyPrefixFor(name));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ Object putIfAbsent(String collectionName, String key, Object value, @Nullable Du
* @param pattern the pattern to clear.
* @return the number of cleared items.
*/
long clear(String pattern);
long clear(String collectionName, String pattern);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.couchbase.client.java.kv.InsertOptions.*;
import static com.couchbase.client.java.kv.UpsertOptions.*;
import static com.couchbase.client.java.query.QueryOptions.*;
import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;

import java.time.Duration;

Expand Down Expand Up @@ -102,10 +103,10 @@ public boolean remove(final String collectionName, final String key) {
}

@Override
public long clear(final String pattern) {
QueryResult result = clientFactory.getCluster().query(
"DELETE FROM `" + clientFactory.getBucket().name() + "` where meta().id LIKE $pattern",
queryOptions().metrics(true).parameters(JsonObject.create().put("pattern", pattern + "%")));
public long clear(final String collectionName, final String pattern) {
QueryResult result = clientFactory.getScope().query(
"DELETE FROM `" + collectionName + "` 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,7 +16,6 @@

package org.springframework.data.couchbase.cache;

import com.couchbase.client.java.query.QueryOptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.couchbase.util.Capabilities;
Expand All @@ -26,7 +25,6 @@

import java.util.UUID;

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 All @@ -47,15 +45,7 @@ public void beforeEach() {
super.beforeEach();
cache = CouchbaseCacheManager.create(couchbaseTemplate.getCouchbaseClientFactory()).createCouchbaseCache("myCache",
CouchbaseCacheConfiguration.defaultCacheConfig().collection("my_collection"));
clear(cache);
}

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));
cache.clear();
}

@Test
Expand All @@ -76,9 +66,21 @@ 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 cacheClear() {
CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1");
CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2");
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
void cacheHitMiss() {
CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1");
Expand Down