DATAREDIS-1082 - Polishing.

Tweak documentation wording. Remove trailing whitespaces. Remove unused methods. Improve method names. Return zero stats when stats do not get collected.

Original Pull Request: #545
This commit is contained in:
Mark Paluch
2020-07-10 14:26:41 +02:00
committed by Christoph Strobl
parent 72c20447b8
commit 1ceb7d4c8b
15 changed files with 119 additions and 57 deletions

View File

@@ -79,7 +79,7 @@ public interface CacheStatistics {
Instant getSince();
/**
* @return instantaneous point in time of last statistics counter reset. Equals {@link #getSince()} if never resetted.
* @return instantaneous point in time of last statistics counter reset. Equals {@link #getSince()} if never reset.
*/
Instant getLastReset();

View File

@@ -26,35 +26,35 @@ public interface CacheStatisticsCollector extends CacheStatisticsProvider {
/**
* Increase the counter for {@literal put operations} of the given cache.
*
*
* @param cacheName must not be {@literal null}.
*/
void incPuts(String cacheName);
/**
* Increase the counter for {@literal get operations} of the given cache.
*
*
* @param cacheName must not be {@literal null}.
*/
void incGets(String cacheName);
/**
* Increase the counter for {@literal get operations with result} of the given cache.
*
*
* @param cacheName must not be {@literal null}.
*/
void incHits(String cacheName);
/**
* Increase the counter for {@literal get operations without result} of the given cache.
*
*
* @param cacheName must not be {@literal null}.
*/
void incMisses(String cacheName);
/**
* Increase the counter for {@literal delete operations} of the given cache.
*
*
* @param cacheName must not be {@literal null}.
*/
default void incDeletes(String cacheName) {
@@ -63,21 +63,21 @@ public interface CacheStatisticsCollector extends CacheStatisticsProvider {
/**
* Increase the counter for {@literal delete operations} of the given cache by the given value.
*
*
* @param cacheName must not be {@literal null}.
*/
void incDeletesBy(String cacheName, int value);
/**
* Increase the gauge for {@literal sync lock duration} of the cache by the given nanoseconds.
*
*
* @param cacheName must not be {@literal null}.
*/
void incLockTime(String cacheName, long durationNS);
/**
* Reset the all counters and gauges of for the given cache.
*
*
* @param cacheName must not be {@literal null}.
*/
void reset(String cacheName);
@@ -92,7 +92,7 @@ public interface CacheStatisticsCollector extends CacheStatisticsProvider {
/**
* @return a default {@link CacheStatisticsCollector} implementation.
*/
static CacheStatisticsCollector instance/*clearlyNeedsBetterNaming*/() {
static CacheStatisticsCollector create() {
return new DefaultCacheStatisticsCollector();
}
}

View File

@@ -16,13 +16,18 @@
package org.springframework.data.redis.cache;
/**
* Interface to be implemented by objects that expose {@link CacheStatistics} identified by {@code cacheName}. Typically
* used by cache writers.
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.4
*/
public interface CacheStatisticsProvider {
/**
* Obtain snapshot of the captured statistics.
* Obtain snapshot of the captured statistics. May return a statistics object whose counters are zero if there are no
* statistics for {@code cacheName}.
*
* @param cacheName must not be {@literal null}.
* @return never {@literal null}.

View File

@@ -21,7 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* Default {@link CacheStatisticsCollector} implementation holding synchronized per cache
* {@link MutableCacheStatistics}.
*
*
* @author Christoph Strobl
* @since 2.4
*/

View File

@@ -244,8 +244,12 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
statistics.reset(name);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.RedisCacheWriter#with(CacheStatisticsCollector)
*/
@Override
public RedisCacheWriter with(CacheStatisticsCollector cacheStatisticsCollector) {
public RedisCacheWriter withStatisticsCollector(CacheStatisticsCollector cacheStatisticsCollector) {
return new DefaultRedisCacheWriter(connectionFactory, sleepTime, cacheStatisticsCollector);
}

View File

@@ -115,10 +115,6 @@ class MutableCacheStatistics implements CacheStatistics {
return deletes.sum();
}
void incDeletes() {
incDeletes(1);
}
/**
* @param x number of removals to add.
*/
@@ -138,11 +134,19 @@ class MutableCacheStatistics implements CacheStatistics {
return unit.convert(lockWaitTimeNs.sum(), TimeUnit.NANOSECONDS);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getSince()
*/
@Override
public Instant getSince() {
return this.aliveSince;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getLastReset()
*/
@Override
public Instant getLastReset() {
return lastReset;
@@ -271,6 +275,9 @@ class MutableCacheStatistics implements CacheStatistics {
*/
@Override
public long getLockWaitDuration(TimeUnit unit) {
Assert.notNull(unit, "TimeUnit must not be null");
return unit.convert(lockWaitTimeNS, TimeUnit.NANOSECONDS);
}

View File

@@ -23,8 +23,9 @@ import org.springframework.util.ObjectUtils;
/**
* {@link CacheStatisticsCollector} implementation that does not capture anything but throws an
* {@link IllegalStateException} when {@link #getCacheStatistics(String) obtaining} {@link CacheStatistics} for a cache.
*
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.4
*/
enum NoOpCacheStatisticsCollector implements CacheStatisticsCollector {
@@ -86,62 +87,102 @@ enum NoOpCacheStatisticsCollector implements CacheStatisticsCollector {
*/
@Override
public CacheStatistics getCacheStatistics(String cacheName) {
return new EmptyStats(cacheName);
return new EmptyStatistics(cacheName);
}
private static class EmptyStats implements CacheStatistics {
private static class EmptyStatistics implements CacheStatistics {
private final String cacheName;
EmptyStats(String cacheName) {
EmptyStatistics(String cacheName) {
this.cacheName = cacheName;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getCacheName()
*/
@Override
public String getCacheName() {
return cacheName;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getPuts()
*/
@Override
public long getPuts() {
return -1;
return 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getGets()
*/
@Override
public long getGets() {
return -1;
return 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getHits()
*/
@Override
public long getHits() {
return -1;
return 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getMisses()
*/
@Override
public long getMisses() {
return -1;
return 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getDeletes()
*/
@Override
public long getDeletes() {
return -1;
return 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getLockWaitDuration(java.util.concurrent.TimeUnit)
*/
@Override
public long getLockWaitDuration(TimeUnit unit) {
return -1;
return 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getSince()
*/
@Override
public Instant getSince() {
return Instant.EPOCH;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.cache.CacheStatistics#getLastReset()
*/
@Override
public Instant getLastReset() {
return getSince();
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
@@ -152,10 +193,14 @@ enum NoOpCacheStatisticsCollector implements CacheStatisticsCollector {
return false;
}
EmptyStats that = (EmptyStats) o;
EmptyStatistics that = (EmptyStatistics) o;
return ObjectUtils.nullSafeEquals(cacheName, that.cacheName);
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(cacheName);

View File

@@ -194,12 +194,11 @@ public class RedisCache extends AbstractValueAdaptingCache {
}
/**
* Return the {@link CacheStatistics} for this cache instance. Statistics are accumulated per cache instance and not
* from the backing Redis data store.
* Return the {@link CacheStatistics} snapshot for this cache instance. Statistics are accumulated per cache instance
* and not from the backing Redis data store.
*
* @return statistics object for this {@link RedisCache}.
* @since 2.4
* @throws IllegalStateException if the {@link RedisCacheWriter} does not support statistics.
*/
public CacheStatistics getStatistics() {
return cacheWriter.getCacheStatistics(getName());

View File

@@ -462,17 +462,8 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
* @since 2.4
*/
public RedisCacheManagerBuilder enableStatistics() {
return statisticsCollector(CacheStatisticsCollector.instance());
}
/**
* @return
* @since 2.4
*/
private RedisCacheManagerBuilder statisticsCollector(CacheStatisticsCollector statisticsCollector) {
Assert.notNull(statisticsCollector, "StatisticsCollector must not be null!");
this.statisticsCollector = statisticsCollector;
this.statisticsCollector = CacheStatisticsCollector.create();
return this;
}
@@ -489,7 +480,7 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
RedisCacheWriter theCacheWriter = cacheWriter;
if (!statisticsCollector.equals(CacheStatisticsCollector.none())) {
theCacheWriter = cacheWriter.with(statisticsCollector);
theCacheWriter = cacheWriter.withStatisticsCollector(statisticsCollector);
}
RedisCacheManager cm = new RedisCacheManager(theCacheWriter, defaultCacheConfiguration, initialCaches,

View File

@@ -120,6 +120,6 @@ public interface RedisCacheWriter extends CacheStatisticsProvider {
* @param cacheStatisticsCollector must not be {@literal null}.
* @return new instance of {@link RedisCacheWriter}.
*/
RedisCacheWriter with(CacheStatisticsCollector cacheStatisticsCollector);
RedisCacheWriter withStatisticsCollector(CacheStatisticsCollector cacheStatisticsCollector);
}