Skip to content

Commit 8029a72

Browse files
authored
Merge pull request #702 from MohammedGhallab/pmd7
Fixing issue 693: Avoid simple caches
2 parents 84fb4b8 + 2b2997c commit 8029a72

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

docs/JavaCodePerformance.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,28 @@ public class CacheKeyGenerator // bad, unclear name
14111411
}
14121412
}
14131413
```
1414+
#### IC18
14141415

1416+
**Observation: Simple caching providers like `SimpleCacheManager` or `ConcurrentMapCache` are used in production.** **Problem:** These simple implementations are intended for testing and prototyping. They lack critical production features such as eviction policies (e.g., LRU), maximum size limits, and manageability. This can lead to memory leaks and `OutOfMemoryError` as the cache grows indefinitely.
1417+
**Solution:** Use a robust, production-ready caching provider like **Caffeine**, **Ehcache**, or a distributed solution like **Redis**.
1418+
**Example:**
1419+
```java
1420+
// Bad: Simple caching with no limits
1421+
@Bean
1422+
public CacheManager cacheManager() {
1423+
return new SimpleCacheManager().setCaches(Arrays.asList(new ConcurrentMapCache("myCache")));
1424+
}
1425+
1426+
// Good: Using Caffeine with size and time limits
1427+
@Bean
1428+
public CacheManager cacheManager() {
1429+
CaffeineCacheManager cacheManager = new CaffeineCacheManager("myCache");
1430+
cacheManager.setCaffeine(Caffeine.newBuilder()
1431+
.maximumSize(1000)
1432+
.expireAfterWrite(10, TimeUnit.MINUTES));
1433+
return cacheManager;
1434+
}
1435+
```
14151436
Too much session usage
14161437
----------------------
14171438

rulesets/java/jpinpoint-java-rules.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6890,7 +6890,7 @@ public ModelAndView initialOverviewRender(RenderRequest request, String viewName
68906890
</rule>
68916891

68926892
<rule name="AvoidSimpleCaches" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="java" message="Avoid simple caching in production"
6893-
externalInfoUrl="https://github.com/jborgers/PMD-jPinpoint-rules/tree/pmd7/docs/JavaCodePerformance.md#ic08">
6893+
externalInfoUrl="https://github.com/jborgers/PMD-jPinpoint-rules/tree/pmd7/docs/JavaCodePerformance.md#ic18">
68946894
<description>Simple caches are used. Problem: Simple caching is meant for testing and prototyping plus it lacks manageability and monitorability.&#13;
68956895
Solution: Use a proper cache implementation like ehcache or a cloud cache.
68966896
(jpinpoint-rules)</description>

src/main/resources/category/java/spring.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ class Good {
426426
</rule>
427427

428428
<rule name="AvoidSimpleCaches" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="java" message="Avoid simple caching in production"
429-
externalInfoUrl="${doc_root}/JavaCodePerformance.md#ic08">
429+
externalInfoUrl="${doc_root}/JavaCodePerformance.md#ic18">
430430
<description>Simple caches are used. Problem: Simple caching is meant for testing and prototyping plus it lacks manageability and monitorability.&#13;
431431
Solution: Use a proper cache implementation like ehcache or a cloud cache.
432432
(jpinpoint-rules)</description>

0 commit comments

Comments
 (0)