Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/JavaCodePerformance.md
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,28 @@ public class CacheKeyGenerator // bad, unclear name
}
}
```
#### IC18

**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.
**Solution:** Use a robust, production-ready caching provider like **Caffeine**, **Ehcache**, or a distributed solution like **Redis**.
**Example:**
```java
// Bad: Simple caching with no limits
@Bean
public CacheManager cacheManager() {
return new SimpleCacheManager().setCaches(Arrays.asList(new ConcurrentMapCache("myCache")));
}

// Good: Using Caffeine with size and time limits
@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("myCache");
cacheManager.setCaffeine(Caffeine.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES));
return cacheManager;
}
```
Too much session usage
----------------------

Expand Down
2 changes: 1 addition & 1 deletion rulesets/java/jpinpoint-java-rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6890,7 +6890,7 @@ public ModelAndView initialOverviewRender(RenderRequest request, String viewName
</rule>

<rule name="AvoidSimpleCaches" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="java" message="Avoid simple caching in production"
externalInfoUrl="https://github.com/jborgers/PMD-jPinpoint-rules/tree/pmd7/docs/JavaCodePerformance.md#ic08">
externalInfoUrl="https://github.com/jborgers/PMD-jPinpoint-rules/tree/pmd7/docs/JavaCodePerformance.md#ic18">
<description>Simple caches are used. Problem: Simple caching is meant for testing and prototyping plus it lacks manageability and monitorability.&#13;
Solution: Use a proper cache implementation like ehcache or a cloud cache.
(jpinpoint-rules)</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/category/java/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class Good {
</rule>

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