-
Notifications
You must be signed in to change notification settings - Fork 5
[ISSUE #36] Fix ConcurrentHashMap memory leak and improve aspect cache performance #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 LY Corporation | ||
* | ||
* LY Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.cse.reqshield.support.config | ||
|
||
/** | ||
* Configuration properties for ReqShield aspect internal caches. | ||
* | ||
* Usage in Spring Boot: | ||
* 1. Add @EnableConfigurationProperties(ReqShieldAspectProperties::class) to your config | ||
* 2. Configure in application.yml: | ||
* req-shield: | ||
* aspect: | ||
* cache-max-size: 500 | ||
*/ | ||
data class ReqShieldAspectProperties( | ||
/** | ||
* Maximum size for aspect internal caches (keyGeneratorMap and reqShieldMap). | ||
* Default: 1000 | ||
*/ | ||
val cacheMaxSize: Int = 1000, | ||
) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||||
/* | ||||||
* Copyright 2024 LY Corporation | ||||||
* | ||||||
* LY Corporation licenses this file to you under the Apache License, | ||||||
* version 2.0 (the "License"); you may not use this file except in compliance | ||||||
* with the License. You may obtain a copy of the License at: | ||||||
* | ||||||
* https://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||||||
* License for the specific language governing permissions and limitations | ||||||
* under the License. | ||||||
*/ | ||||||
|
||||||
package com.linecorp.cse.reqshield.support.utils | ||||||
|
||||||
/** | ||||||
* High-performance LRU Cache with O(1) operations. | ||||||
* Based on LinkedHashMap with access-order for optimal LRU behavior. | ||||||
* | ||||||
* This implementation provides better performance than the previous AtomicLong-based version | ||||||
* by leveraging LinkedHashMap's built-in LRU behavior. | ||||||
*/ | ||||||
class LRUCache<K, V>(private val maxSize: Int) { | ||||||
private val cache = | ||||||
object : LinkedHashMap<K, V>(maxSize + 1, 0.75f, true) { | ||||||
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<K, V>?): Boolean { | ||||||
return size > maxSize | ||||||
} | ||||||
} | ||||||
|
||||||
@Synchronized | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most methods are using |
||||||
fun computeIfAbsent( | ||||||
key: K, | ||||||
mappingFunction: (K) -> V, | ||||||
): V { | ||||||
return cache.computeIfAbsent(key, mappingFunction) | ||||||
} | ||||||
|
||||||
@Synchronized | ||||||
operator fun get(key: K): V? { | ||||||
return cache[key] | ||||||
} | ||||||
|
||||||
@Synchronized | ||||||
fun put( | ||||||
key: K, | ||||||
value: V, | ||||||
): V? { | ||||||
return cache.put(key, value) | ||||||
} | ||||||
|
||||||
@Synchronized | ||||||
fun remove(key: K): V? { | ||||||
return cache.remove(key) | ||||||
} | ||||||
|
||||||
@Synchronized | ||||||
fun clear() { | ||||||
cache.clear() | ||||||
} | ||||||
|
||||||
@Synchronized | ||||||
fun keys(): Set<K> = LinkedHashSet(cache.keys) | ||||||
|
||||||
@Synchronized | ||||||
fun size(): Int = cache.size | ||||||
|
||||||
val size: Int get() = cache.size | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a default parameter with constructor instantiation bypasses Spring's dependency injection mechanism. This means the configuration properties won't be properly injected from application properties. Consider making this parameter required or using
@Autowired
to ensure proper Spring configuration.Copilot uses AI. Check for mistakes.