Skip to content

Commit 7b5b569

Browse files
committed
New property: cacheLockTimeout
1 parent 1d800cf commit 7b5b569

15 files changed

+103
-49
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.github.mihaicostin</groupId>
66
<artifactId>hibernate-l2-memcached</artifactId>
7-
<version>5.2.7.1</version>
7+
<version>5.2.10.0</version>
88
<name>hibernate-l2-memcached</name>
99
<description>A library for using Memcached as a second level distributed cache in Hibernate.</description>
1010
<url>https://github.com/mihaicostin/hibernate-l2-memcached</url>
@@ -39,8 +39,8 @@
3939
</distributionManagement>
4040

4141
<properties>
42-
<hibernate-core.version>5.2.7.Final</hibernate-core.version>
43-
<spymemcached.version>2.12.2</spymemcached.version>
42+
<hibernate-core.version>5.2.10.Final</hibernate-core.version>
43+
<spymemcached.version>2.12.3</spymemcached.version>
4444
<slf4j-api.version>1.5.6</slf4j-api.version>
4545

4646
<junit.version>4.12</junit.version>

src/main/java/com/mc/hibernate/memcached/Config.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ public class Config {
2727
public static final String PROP_PREFIX = "hibernate.memcached.";
2828

2929
private static final String CACHE_TIME_SECONDS = "cacheTimeSeconds";
30+
private static final String LOCK_TIMEOUT_MS = "cacheLockTimeout";
3031
private static final String CLEAR_SUPPORTED = "clearSupported";
3132
private static final String MEMCACHE_CLIENT_FACTORY = "memcacheClientFactory";
3233
private static final String DOGPILE_PREVENTION = "dogpilePrevention";
3334
private static final String DOGPILE_PREVENTION_EXPIRATION_FACTOR = "dogpilePrevention.expirationFactor";
3435
private static final String KEY_STRATEGY = "keyStrategy";
3536

3637
private static final int DEFAULT_CACHE_TIME_SECONDS = 300;
38+
private static final int DEFAULT_CACHE_LOCK_TIMEOUT_MS = 60000;
3739
private static final boolean DEFAULT_CLEAR_SUPPORTED = false;
3840
private static final boolean DEFAULT_DOGPILE_PREVENTION = false;
3941
private static final int DEFAULT_DOGPILE_EXPIRATION_FACTOR = 2;
@@ -50,6 +52,11 @@ public int getCacheTimeSeconds(String cacheRegion) {
5052
return props.getInt(cacheRegionPrefix(cacheRegion) + CACHE_TIME_SECONDS, globalCacheTimeSeconds);
5153
}
5254

55+
public int getCacheLockTimeout(String cacheRegion) {
56+
int globalLockTimeout = props.getInt(PROP_PREFIX + LOCK_TIMEOUT_MS, DEFAULT_CACHE_LOCK_TIMEOUT_MS);
57+
return props.getInt(cacheRegionPrefix(cacheRegion) + LOCK_TIMEOUT_MS, globalLockTimeout);
58+
}
59+
5360
public String getKeyStrategyName(String cacheRegion) {
5461
String globalKeyStrategy = props.get(PROP_PREFIX + KEY_STRATEGY, Sha1KeyStrategy.class.getName());
5562
return props.get(cacheRegionPrefix(cacheRegion) + KEY_STRATEGY, globalKeyStrategy);

src/main/java/com/mc/hibernate/memcached/MemcachedCache.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,6 @@ public void lock(Object key) throws CacheException {
217217
public void unlock(Object key) throws CacheException {
218218
}
219219

220-
public long nextTimestamp() {
221-
return System.currentTimeMillis() / 100;
222-
}
223-
224-
public int getTimeout() {
225-
return cacheTimeSeconds;
226-
}
227-
228220
public String getRegionName() {
229221
return regionName;
230222
}

src/main/java/com/mc/hibernate/memcached/MemcachedRegionFactory.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void start(SessionFactoryOptions settings, Properties properties) throws
5252
this.properties = properties;
5353
log.info("Starting MemcachedClient...");
5454
try {
55-
client = getMemcachedClientFactory(new Config(new PropertiesHelper(properties))).createMemcacheClient();
55+
client = getMemcachedClientFactory(wrapInConfig(properties)).createMemcacheClient();
5656
} catch (Exception e) {
5757
throw new CacheException("Unable to initialize MemcachedClient", e);
5858
}
@@ -75,30 +75,30 @@ public AccessType getDefaultAccessType() {
7575
}
7676

7777
public long nextTimestamp() {
78-
return System.currentTimeMillis() / 100;
78+
return System.currentTimeMillis();
7979
}
8080

8181
public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
82-
return new MemcachedEntityRegion(getCache(regionName), settings, metadata, properties, client);
82+
return new MemcachedEntityRegion(getCache(regionName), settings, metadata, wrapInConfig(properties));
8383
}
8484

8585
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
86-
return new MemcachedNaturalIdRegion(getCache(regionName), settings, metadata, properties, client);
86+
return new MemcachedNaturalIdRegion(getCache(regionName), settings, metadata, wrapInConfig(properties));
8787
}
8888

8989
public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
90-
return new MemcachedCollectionRegion(getCache(regionName), settings, metadata, properties, client);
90+
return new MemcachedCollectionRegion(getCache(regionName), settings, metadata, wrapInConfig(properties));
9191
}
9292

9393
public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException {
94-
return new MemcachedQueryResultsRegion(getCache(regionName));
94+
return new MemcachedQueryResultsRegion(getCache(regionName), wrapInConfig(properties));
9595
}
9696

9797
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
98-
return new MemcachedTimestampsRegion(getCache(regionName));
98+
return new MemcachedTimestampsRegion(getCache(regionName), wrapInConfig(properties));
9999
}
100100

101-
protected MemcacheClientFactory getMemcachedClientFactory(Config config) {
101+
private MemcacheClientFactory getMemcachedClientFactory(Config config) {
102102
String factoryClassName = config.getMemcachedClientFactoryName();
103103

104104
Constructor<?> constructor;
@@ -123,4 +123,9 @@ protected MemcacheClientFactory getMemcachedClientFactory(Config config) {
123123
private MemcachedCache getCache(String regionName) {
124124
return caches.get(regionName) == null ? new MemcachedCache(regionName, client, new Config(new PropertiesHelper(properties))) : caches.get(regionName);
125125
}
126+
127+
private Config wrapInConfig(Properties properties) {
128+
return new Config(new PropertiesHelper(properties));
129+
}
130+
126131
}

src/main/java/com/mc/hibernate/memcached/region/AbstractMemcachedRegion.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@
1515

1616
package com.mc.hibernate.memcached.region;
1717

18+
import com.mc.hibernate.memcached.Config;
1819
import com.mc.hibernate.memcached.MemcachedCache;
1920
import org.hibernate.cache.CacheException;
2021
import org.hibernate.cache.spi.Region;
2122

23+
import java.util.Collections;
2224
import java.util.Map;
2325

2426
public abstract class AbstractMemcachedRegion implements Region {
2527

28+
private final int lockTimeout;
2629
protected MemcachedCache cache;
2730

28-
AbstractMemcachedRegion(MemcachedCache cache) {
31+
AbstractMemcachedRegion(MemcachedCache cache, Config config) {
2932
this.cache = cache;
33+
this.lockTimeout = config.getCacheLockTimeout(cache.getRegionName());
3034
}
3135

3236
public String getName() {
@@ -53,17 +57,28 @@ public long getElementCountOnDisk() {
5357
return cache.getElementCountOnDisk();
5458
}
5559

60+
/**
61+
* {@inheritDoc}
62+
*
63+
* @return emptyMap, since memcached doesn't support this notion.
64+
*/
5665
@Override
5766
public Map toMap() {
58-
return cache.toMap();
67+
return Collections.emptyMap();
5968
}
6069

70+
/**
71+
* @return next timestamp in ms
72+
*/
6173
public long nextTimestamp() {
62-
return System.currentTimeMillis() / 100;
74+
return System.currentTimeMillis();
6375
}
6476

77+
/**
78+
* @return cache lock timeout in ms
79+
*/
6580
public int getTimeout() {
66-
return cache.getTimeout();
81+
return lockTimeout;
6782
}
6883

6984
public MemcachedCache getCache() {

src/main/java/com/mc/hibernate/memcached/region/MemcachedCollectionRegion.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
package com.mc.hibernate.memcached.region;
1717

18-
import com.mc.hibernate.memcached.Memcache;
18+
import com.mc.hibernate.memcached.Config;
1919
import com.mc.hibernate.memcached.MemcachedCache;
2020
import com.mc.hibernate.memcached.strategy.NonStrictReadWriteMemcachedCollectionRegionAccessStrategy;
2121
import com.mc.hibernate.memcached.strategy.ReadOnlyMemcachedCollectionRegionAccessStrategy;
@@ -30,16 +30,14 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

33-
import java.util.Properties;
34-
3533
public class MemcachedCollectionRegion extends AbstractMemcachedRegion implements CollectionRegion {
3634

3735
private final Logger log = LoggerFactory.getLogger(MemcachedCollectionRegion.class);
3836
private final CacheDataDescription metadata;
3937
private final SessionFactoryOptions settings;
4038

41-
public MemcachedCollectionRegion(MemcachedCache cache, SessionFactoryOptions settings, CacheDataDescription metadata, Properties properties, Memcache client) {
42-
super(cache);
39+
public MemcachedCollectionRegion(MemcachedCache cache, SessionFactoryOptions settings, CacheDataDescription metadata, Config config) {
40+
super(cache, config);
4341
this.metadata = metadata;
4442
this.settings = settings;
4543
}

src/main/java/com/mc/hibernate/memcached/region/MemcachedEntityRegion.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
package com.mc.hibernate.memcached.region;
1717

18-
import com.mc.hibernate.memcached.Memcache;
18+
import com.mc.hibernate.memcached.Config;
1919
import com.mc.hibernate.memcached.MemcachedCache;
2020
import com.mc.hibernate.memcached.strategy.NonStrictReadWriteMemcachedEntityRegionAccessStrategy;
2121
import com.mc.hibernate.memcached.strategy.ReadOnlyMemcachedEntityRegionAccessStrategy;
@@ -30,17 +30,15 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

33-
import java.util.Properties;
34-
3533
public class MemcachedEntityRegion extends AbstractMemcachedRegion implements EntityRegion {
3634

3735
private final Logger log = LoggerFactory.getLogger(MemcachedEntityRegion.class);
3836

3937
private final CacheDataDescription metadata;
4038
private final SessionFactoryOptions settings;
4139

42-
public MemcachedEntityRegion(MemcachedCache cache, SessionFactoryOptions settings, CacheDataDescription metadata, Properties properties, Memcache client) {
43-
super(cache);
40+
public MemcachedEntityRegion(MemcachedCache cache, SessionFactoryOptions settings, CacheDataDescription metadata, Config config) {
41+
super(cache, config);
4442
this.metadata = metadata;
4543
this.settings = settings;
4644
}

src/main/java/com/mc/hibernate/memcached/region/MemcachedNaturalIdRegion.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
package com.mc.hibernate.memcached.region;
1717

18-
import com.mc.hibernate.memcached.Memcache;
18+
import com.mc.hibernate.memcached.Config;
1919
import com.mc.hibernate.memcached.MemcachedCache;
2020
import com.mc.hibernate.memcached.strategy.NonStrictReadWriteMemcachedNaturalIdRegionAccessStrategy;
2121
import com.mc.hibernate.memcached.strategy.ReadOnlyMemcachedNaturalIdRegionAccessStrategy;
@@ -30,17 +30,15 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

33-
import java.util.Properties;
34-
3533
public class MemcachedNaturalIdRegion extends AbstractMemcachedRegion implements NaturalIdRegion {
3634

3735
private final Logger log = LoggerFactory.getLogger(MemcachedNaturalIdRegion.class);
3836

3937
private final CacheDataDescription metadata;
4038
private final SessionFactoryOptions settings;
4139

42-
public MemcachedNaturalIdRegion(MemcachedCache cache, SessionFactoryOptions settings, CacheDataDescription metadata, Properties properties, Memcache client) {
43-
super(cache);
40+
public MemcachedNaturalIdRegion(MemcachedCache cache, SessionFactoryOptions settings, CacheDataDescription metadata, Config config) {
41+
super(cache, config);
4442
this.metadata = metadata;
4543
this.settings = settings;
4644
}

src/main/java/com/mc/hibernate/memcached/region/MemcachedQueryResultsRegion.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515

1616
package com.mc.hibernate.memcached.region;
1717

18+
import com.mc.hibernate.memcached.Config;
1819
import com.mc.hibernate.memcached.MemcachedCache;
1920
import org.hibernate.cache.CacheException;
2021
import org.hibernate.cache.spi.QueryResultsRegion;
2122
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2223

2324
public class MemcachedQueryResultsRegion extends AbstractMemcachedRegion implements QueryResultsRegion {
2425

25-
public MemcachedQueryResultsRegion(MemcachedCache cache) {
26-
super(cache);
26+
public MemcachedQueryResultsRegion(MemcachedCache cache, Config config) {
27+
super(cache, config);
2728
}
2829

2930
@Override

src/main/java/com/mc/hibernate/memcached/region/MemcachedTimestampsRegion.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515

1616
package com.mc.hibernate.memcached.region;
1717

18+
import com.mc.hibernate.memcached.Config;
1819
import com.mc.hibernate.memcached.MemcachedCache;
1920
import org.hibernate.cache.CacheException;
2021
import org.hibernate.cache.spi.TimestampsRegion;
2122
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2223

2324
public class MemcachedTimestampsRegion extends AbstractMemcachedRegion implements TimestampsRegion {
2425

25-
public MemcachedTimestampsRegion(MemcachedCache cache) {
26-
super(cache);
26+
public MemcachedTimestampsRegion(MemcachedCache cache, Config config) {
27+
super(cache, config);
2728
}
2829

2930
@Override

0 commit comments

Comments
 (0)