Skip to content

Allow caching null objects. #1303

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
Jan 17, 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 @@ -106,7 +106,7 @@ public void put(final Object key, final Object value) {
name));
}

cacheWriter.put(cacheConfig.getCollectionName(), createCacheKey(key), value, cacheConfig.getExpiry(),
cacheWriter.put(cacheConfig.getCollectionName(), createCacheKey(key), toStoreValue(value), cacheConfig.getExpiry(),
cacheConfig.getValueTranscoder());
}

Expand All @@ -116,7 +116,7 @@ public ValueWrapper putIfAbsent(final Object key, final Object value) {
return get(key);
}

Object result = cacheWriter.putIfAbsent(cacheConfig.getCollectionName(), createCacheKey(key), value,
Object result = cacheWriter.putIfAbsent(cacheConfig.getCollectionName(), createCacheKey(key), toStoreValue(value),
cacheConfig.getExpiry(), cacheConfig.getValueTranscoder());

if (result == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.cache;

import java.io.Serializable;

/**
* This is a standalone class (vs. inner) to allow Serialization of all fields to work.
* If it was an inner class of CouchbaseCacheIntegrationTests, then it would have a
* this$0 field = CouchbaseCacheIntegrationTests and would not serialize.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If CacheUser were a static nested class, would it it still need to be in a separate file?

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

*
* @author Michael Reiche
*/
class CacheUser implements Serializable {
// private static final long serialVersionUID = 8817717605659870262L;
String firstname;
String lastname;
String id;

public CacheUser(String id, String firstname, String lastname) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
}

public String getId() {
return id;
}

// define equals for assertEquals()
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof CacheUser)) {
return false;
}

CacheUser other = (CacheUser) o;
if (id == null && other.id != null) {
return false;
}
if (firstname == null && other.firstname != null) {
return false;
}
if (lastname == null && other.lastname != null) {
return false;
}
return id.equals(other.id) && firstname.equals(other.firstname) && lastname.equals(other.lastname);
}

public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("CacheUser: { id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + "}");
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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;

import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
import org.springframework.data.couchbase.util.JavaIntegrationTests;

import com.couchbase.client.java.query.QueryOptions;

/**
* CouchbaseCache tests Theses tests rely on a cb server running.
*
* @author Michael Reiche
*/
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
class CouchbaseCacheIntegrationTests extends JavaIntegrationTests {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are good. Are they related to the "Allow caching null objects" change?

Should we have a test that caches and retrieves a null value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cacheHitMiss() tests a cache hit on a stored null.
It also tests a cache-miss - which is the same for a null as a non-null.
The other tests have cache-hits on non-null (the normal case).


volatile CouchbaseCache cache;

@BeforeEach
@Override
public void beforeEach() {
super.beforeEach();
cache = CouchbaseCacheManager.create(couchbaseTemplate.getCouchbaseClientFactory()).createCouchbaseCache("myCache",
CouchbaseCacheConfiguration.defaultCacheConfig());
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));
}

@Test
void cachePutGet() {
CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1");
CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2");
assertNull(cache.get(user1.getId())); // was not put -> cacheMiss
cache.put(user1.getId(), user1); // put user1
cache.put(user2.getId(), user2); // put user2
assertEquals(user1, cache.get(user1.getId()).get()); // get user1
assertEquals(user2, cache.get(user2.getId()).get()); // get user2
}

@Test
void cacheEvict() {
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.evict(user1.getId()); // evict user1
assertEquals(user2, cache.get(user2.getId()).get()); // get user2 -> present
}

@Test
void cacheHitMiss() {
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
}

@Test
void cachePutIfAbsent() {
CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1");
CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2");
assertNull(cache.putIfAbsent(user1.getId(), user1)); // should put user1, return null
assertEquals(user1, cache.putIfAbsent(user1.getId(), user2).get()); // should not put user2, should return user1
assertEquals(user1, cache.get(user1.getId()).get()); // user1.getId() is still user1
}

}