Skip to content

Commit 7996250

Browse files
author
Igor Polevoy
committed
#459 Implement possibility to stop cache event propagation
1 parent e8c0e28 commit 7996250

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/cache/CacheEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public enum CacheEventType{
5353
*/
5454
public CacheEvent( String group, String source){
5555
if(group == null)
56-
throw new IllegalArgumentException("group canot be null");
56+
throw new IllegalArgumentException("group cannot be null");
5757

5858
this.type = CacheEventType.GROUP;
5959
this.source = source;

activejdbc/src/main/java/org/javalite/activejdbc/cache/CacheManager.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,42 @@ public abstract class CacheManager {
5656

5757

5858
/**
59-
* Flash cache.
59+
* Flashes cache.
6060
*
61+
* @param propagate true to propagate event to listeners, false to not propagate
6162
* @param event type of caches to flush.
6263
*/
63-
public final void flush(CacheEvent event){
64+
public final void flush(CacheEvent event, boolean propagate){
6465
doFlush(event);
66+
if(propagate){
67+
propagate(event);
68+
}
69+
70+
if (logger.isInfoEnabled()) {
71+
String message = "Cache purged: " + (event.getType() == CacheEvent.CacheEventType.ALL
72+
? "all caches" : "table: " + event.getGroup());
73+
LogFilter.log(logger, message);
74+
}
75+
}
76+
77+
private void propagate(CacheEvent event){
6578
for(CacheEventListener listener: listeners){
6679
try{
6780
listener.onFlush(event);
6881
}catch(Exception e){
6982
logger.warn("failed to propagate cache event: {} to listener: {}", event, listener, e);
7083
}
7184
}
72-
if (logger.isInfoEnabled()) {
73-
String message = "Cache purged: " + (event.getType() == CacheEvent.CacheEventType.ALL
74-
? "all caches" : "table: " + event.getGroup());
75-
LogFilter.log(logger, message);
76-
}
85+
}
86+
87+
88+
/**
89+
* Flashes cache.
90+
*
91+
* @param event type of caches to flush.
92+
*/
93+
public final void flush(CacheEvent event){
94+
flush(event, true);
7795
}
7896

7997
public final void addCacheEventListener(CacheEventListener listener){

activejdbc/src/test/java/org/javalite/activejdbc/CacheTest.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import java.util.List;
3030

31+
import static org.javalite.test.jspec.JSpec.$;
32+
3133

3234
/**
3335
* @author Igor Polevoy
@@ -156,22 +158,33 @@ public void shouldNotAddInfoToStatisticsIfFoundResultInCache(){
156158
int count = 0;
157159
@Test
158160
public void shouldNotPropagateCacheEventForNonCachedModels(){
159-
160161
CacheEventListener cl = new CacheEventListener() {
161162
public void onFlush(CacheEvent event) {
162163
count++;
163164
}
164165
};
165-
166-
QueryCache.instance().getCacheManager().addCacheEventListener(cl);
166+
Registry.cacheManager().addCacheEventListener(cl);
167167
Person.deleteAll();
168168
a(count).shouldBeEqual(1);
169+
Account.deleteAll();
170+
a(count).shouldBeEqual(1);
171+
}
172+
173+
int count1 = 0;
174+
@Test
175+
public void shouldNotPropagateCacheEventOnFlush(){
176+
CacheEventListener cl = new CacheEventListener() {
177+
public void onFlush(CacheEvent event) {
178+
count1++;
179+
}
180+
};
181+
Registry.cacheManager().addCacheEventListener(cl);
169182

183+
Registry.cacheManager().flush(new CacheEvent("people", "blah"), false);
170184

171-
Account.deleteAll();
185+
$(count1).shouldBeEqual(0);
172186

173-
a(count).shouldBeEqual(1);
174187

175-
}
176188

189+
}
177190
}

0 commit comments

Comments
 (0)