Skip to content

Commit fd88842

Browse files
committed
make region.nextTimestamp() and region.getTimeout() consistent
1 parent db84872 commit fd88842

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void lock(Object key) throws CacheException {
217217
public void unlock(Object key) throws CacheException {
218218
}
219219

220-
public int getTimeout() {
220+
public int getTimeoutSeconds() {
221221
return cacheTimeSeconds;
222222
}
223223

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ public AccessType getDefaultAccessType() {
7373
return AccessType.READ_WRITE;
7474
}
7575

76+
/**
77+
* {@inheritDoc}
78+
*
79+
* @return timestamp in ms
80+
*/
7681
public long nextTimestamp() {
77-
return System.currentTimeMillis() / 100;
82+
return System.currentTimeMillis();
7883
}
7984

8085
public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {

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

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

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

18-
import java.util.Map;
19-
18+
import com.mc.hibernate.memcached.MemcachedCache;
2019
import org.hibernate.cache.CacheException;
2120
import org.hibernate.cache.spi.Region;
2221

23-
import com.mc.hibernate.memcached.MemcachedCache;
22+
import java.util.Collections;
23+
import java.util.Map;
2424

2525
public abstract class AbstractMemcachedRegion implements Region {
2626

@@ -55,15 +55,25 @@ public long getElementCountOnDisk() {
5555
}
5656

5757
public Map toMap() {
58-
return null;
58+
return Collections.emptyMap();
5959
}
6060

61+
/**
62+
* {@inheritDoc}
63+
*
64+
* @return timestamp in ms
65+
*/
6166
public long nextTimestamp() {
62-
return System.currentTimeMillis() / 100;
67+
return System.currentTimeMillis();
6368
}
6469

70+
/**
71+
* {@inheritDoc}
72+
*
73+
* @return timeout in ms
74+
*/
6575
public int getTimeout() {
66-
return cache.getTimeout();
76+
return cache.getTimeoutSeconds() * 1000;
6777
}
6878

6979
public MemcachedCache getCache() {

src/test/java/com/integration/com/mc/hibernate/memcached/CacheTimeout.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void testQueryCacheQuickExpire() throws Exception {
1919

2020
SessionFactory sessionFactory = getConfiguration(extraProp).buildSessionFactory();
2121

22-
Person p = new Person(10L, "Jhon");
22+
Person p = new Person(10L, "John");
2323

2424
Session session = sessionFactory.openSession();
2525
Transaction transaction = session.beginTransaction();
@@ -38,6 +38,34 @@ public void testQueryCacheQuickExpire() throws Exception {
3838

3939
}
4040

41+
42+
@Test
43+
public void testQueryCacheSlowExpire() throws Exception {
44+
45+
Properties extraProp = new Properties();
46+
extraProp.put("hibernate.memcached.com.integration.com.mc.hibernate.memcached.entities.Person.cacheTimeSeconds", "5");
47+
48+
SessionFactory sessionFactory = getConfiguration(extraProp).buildSessionFactory();
49+
50+
Person p = new Person(101L, "Long John");
51+
52+
Session session = sessionFactory.openSession();
53+
Transaction transaction = session.beginTransaction();
54+
session.save(p);
55+
transaction.commit();
56+
57+
session.get(Person.class, 101L);
58+
59+
boolean containsEntity = sessionFactory.getCache().containsEntity(Person.class, 101L);
60+
Assert.assertTrue(containsEntity);
61+
62+
Thread.sleep(3000);
63+
64+
containsEntity = sessionFactory.getCache().containsEntity(Person.class, 101L);
65+
Assert.assertTrue(containsEntity);
66+
67+
}
68+
4169
@Test
4270
public void testNoExpireProvided() throws Exception {
4371

0 commit comments

Comments
 (0)