![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
github.com/emersonary/heapedcache
HeapedCache
is a generic, extremely fast, thread-safe in-memory cache for Go that maintains a fixed size by automatically removing the least recently accessed items when the cache limit is reached. This package leverages a min-heap to efficiently manage cache eviction based on item age.
maxRows
). When this limit is reached, the oldest item is automatically removed to make space for new entries. The cache is intended to be initialized at the start of the application, allocating the desired amount of memory upfront. There is no capacity resizing, which ensures maximum performance.
container/heap
library, with all its operations having a time complexity of O(log n). Both the map and the slice store pointers to the cached objects, facilitating efficient access and management.
[T any]
), the cache can store any type of object, providing flexibility and type safety.
In a 64 bit application, it consumes 16 bytes per item. That means that if you store 1 million items, it allocates 16 MB, represented by a list of pointers to the desired structure, whose (unknown) allocated memory is not referenced on this estimation.
Tests were performed in the environment below:
import "path/to/util"
type Person struct {
Id int
Name string
Phone string
}
cache := util.NewHeapedCache[Person]( 1000000 ) // Cache with 1 million items
obj := &Person{/* ... */}
cache.Push(obj.Id, obj)
obj := cache.Get(itemId)
if obj != nil {
// Use the cached object
}
If the item is not found in the cache, GetOrPush
can be used to fetch or create it:
obj := cache.GetOrPush(itemId, func(id any) *Person {
// Logic to load/create the item if it's not in the cache
return &Person{/* ... */}
})
removed := cache.Remove(itemId)
if removed {
// Item was successfully removed
}
size := cache.Len()
NewHeapedCache[T any](maxRows int) *HeapedCache[T]
Creates a new HeapedCache
with a fixed maximum size.
Push(id any, item *T) *T
Adds an item to the cache or updates it if it already exists. If the cache is full, the oldest item is evicted.
Get(id any) *T
Retrieves an item from the cache by its ID. Returns nil
if the item is not found.
GetOrPush(id any, fn func(id any) *T) *T
Retrieves an item from the cache by its ID. If the item does not exist, the provided function fn
is called to create it, and the new item is added to the cache.
Remove(id any) bool
Removes an item from the cache by its ID. Returns true
if the item was successfully removed.
Len() int
Returns the number of items currently stored in the cache.
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss any changes.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.