Skip to content

Commit 24ee213

Browse files
committed
v1.5.2: strategy clear cache
1 parent efa70d6 commit 24ee213

File tree

5 files changed

+76
-16
lines changed

5 files changed

+76
-16
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ for a higher control compared to the EasyML Facade.
6060

6161
### Release Notes
6262

63+
!Release 1.5.2
64+
- feature: added generic mechanism for cache clearing.
65+
66+
6367
!Release 1.5.1
64-
- feature: add functional API.
65-
- feature: add CalendarStrategy and OptionalStrategy.
68+
- feature: added functional API.
69+
- feature: added CalendarStrategy and OptionalStrategy.
6670
- feature: added java.time strategies.
6771
- performance: added caching to SerializableStrategy.
6872
- refactor: remove Profile feature.

easyml/src/net/sourceforge/easyml/XMLReader.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
*/
1919
package net.sourceforge.easyml;
2020

21-
import net.sourceforge.easyml.marshalling.CompositeReader;
22-
import net.sourceforge.easyml.marshalling.CompositeStrategy;
23-
import net.sourceforge.easyml.marshalling.SimpleStrategy;
24-
import net.sourceforge.easyml.marshalling.UnmarshalContext;
21+
import net.sourceforge.easyml.marshalling.*;
2522
import net.sourceforge.easyml.marshalling.dtd.*;
2623
import net.sourceforge.easyml.marshalling.java.io.SerializableStrategy;
2724
import net.sourceforge.easyml.marshalling.java.lang.*;
@@ -1090,6 +1087,7 @@ public void reset(Document in) {
10901087
*/
10911088
public void clearCache() {
10921089
this.checkNotSharedConfiguration();
1090+
// clear class and field cache:
10931091
final Iterator<Map.Entry<String, Object>> iter = this.cachedAliasingReflection.entrySet().iterator();
10941092
while (iter.hasNext()) {
10951093
final Map.Entry<String, Object> crt = iter.next();
@@ -1106,11 +1104,9 @@ public void clearCache() {
11061104
iter.remove(); // removed non-alias entry.
11071105
}
11081106
}
1109-
// clear SerializableStrategy cache if present:
1110-
final CompositeStrategy serialStrategy = this.compositeStrategies.get(SerializableStrategy.NAME);
1111-
if (serialStrategy instanceof SerializableStrategy) {
1112-
((SerializableStrategy) serialStrategy).clearCache();
1113-
}
1107+
// clear strategies cache:
1108+
this.simpleStrategies.values().forEach(XMLReader::clearCache);
1109+
this.compositeStrategies.values().forEach(XMLReader::clearCache);
11141110
}
11151111

11161112
private static String fieldNameFrom(String fieldFQN) {
@@ -1121,6 +1117,12 @@ private static boolean isCacheEntry(String key, String name) {
11211117
return key.equals(name);
11221118
}
11231119

1120+
private static void clearCache(Strategy s) {
1121+
if (s instanceof Caching) {
1122+
((Caching) s).clearCache();
1123+
}
1124+
}
1125+
11241126
/**
11251127
* Closes this instance by releasing all resources.
11261128
*/

easyml/src/net/sourceforge/easyml/XMLWriter.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ public boolean remove(S s) {
201201
}
202202
}
203203

204+
/**
205+
* Applies the consumer on all registered strategies.
206+
*/
207+
public void forEach(Consumer<S> consumer) {
208+
this.strict.forEach((k, v) -> consumer.accept(v));
209+
this.range.forEach(consumer);
210+
if (this.backup != null) {
211+
this.backup.forEach(consumer);
212+
}
213+
}
214+
204215
/**
205216
* Clears this instance of all strategies.
206217
*/
@@ -1022,10 +1033,15 @@ public void reset(Document out) {
10221033
*/
10231034
public void clearCache() {
10241035
this.checkNotSharedConfiguration();
1025-
// clear SerializableStrategy cache if present:
1026-
final CompositeStrategy serialStrategy = this.compositeStrategies.lookup(SerializableStrategy.TARGET);
1027-
if (serialStrategy instanceof SerializableStrategy) {
1028-
((SerializableStrategy) serialStrategy).clearCache();
1036+
// no class and field cache.
1037+
// clear strategies cache:
1038+
this.simpleStrategies.forEach(XMLWriter::clearCache);
1039+
this.compositeStrategies.forEach(XMLWriter::clearCache);
1040+
}
1041+
1042+
private static void clearCache(Strategy s) {
1043+
if (s instanceof Caching) {
1044+
((Caching) s).clearCache();
10291045
}
10301046
}
10311047

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012 Victor Cordis
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Please contact the author ( [email protected] ) if you need additional
17+
* information or have any questions.
18+
*/
19+
package net.sourceforge.easyml.marshalling;
20+
21+
/**
22+
* Caching interface defines objects which do caching.
23+
*
24+
* @author Victor Cordis ( cordis.victor at gmail.com)
25+
* @version 1.5.2
26+
* @since 1.5.2
27+
*/
28+
public interface Caching {
29+
30+
/**
31+
* Clears the cache of this instance.
32+
*/
33+
void clearCache();
34+
}

easyml/src/net/sourceforge/easyml/marshalling/java/io/SerializableStrategy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* @version 1.5.1
5555
* @since 1.0
5656
*/
57-
public final class SerializableStrategy extends AbstractStrategy implements CompositeStrategy<Serializable> {
57+
public final class SerializableStrategy extends AbstractStrategy implements CompositeStrategy<Serializable>, Caching {
5858
/**
5959
* Constant defining the value used for the strategy name.
6060
*/
@@ -97,6 +97,10 @@ public SerializableStrategy() {
9797
readObjectCache = new ConcurrentHashMap<>();
9898
}
9999

100+
/**
101+
* {@inheritDoc}
102+
*/
103+
@Override
100104
public void clearCache() {
101105
writeReplaceCache.clear();
102106
writeObjectCache.clear();

0 commit comments

Comments
 (0)