https://github.com/knbr13/incache
Simple, fast, concurrent in-memory database.
https://github.com/knbr13/incache
blazingly-fast cache db go golang inmemory-db thread-safe
Last synced: 10 months ago
JSON representation
Simple, fast, concurrent in-memory database.
- Host: GitHub
- URL: https://github.com/knbr13/incache
- Owner: knbr13
- License: mit
- Created: 2023-06-22T00:35:27.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-09T20:29:41.000Z (about 2 years ago)
- Last Synced: 2025-06-12T05:54:18.520Z (about 1 year ago)
- Topics: blazingly-fast, cache, db, go, golang, inmemory-db, thread-safe
- Language: Go
- Homepage:
- Size: 900 KB
- Stars: 14
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## incache
The `incache` package provides a simple cache implementation in Go. It can be used as a package in other Go projects to store key-value pairs in memory. The package is safe to use concurrently with multiple goroutines.
### Installation
To use this package in your Go project, you can install it using `go get`:
```bash
go get github.com/knbr13/incache
```
### Example
```go
package main
import (
"fmt"
"time"
"github.com/knbr13/incache"
)
func main() {
// create a new LRU Cache
c := incache.NewLRU[string, int](10)
fmt.Println("keys:", c.Keys())
// set some key-value pairs
c.Set("one", 1)
c.Set("two", 2)
c.Set("three", 3)
c.Set("four", 4)
c.Set("five", 5)
c.SetWithTimeout("six", 6, time.Millisecond)
// Get values by key
v, ok := c.Get("one")
if ok {
fmt.Println("Value for key1:", v)
}
v, ok = c.Get("two")
if ok {
fmt.Println("Value for key1:", v)
}
c.Delete("one")
// create new cache, move data from 'c' to 'c2'
c2 := incache.NewLRU[string, int](10)
c.TransferTo(c2)
// create new cache, copy data from 'c2' to 'c3'
c3 := incache.NewLRU[string, int](10)
c2.CopyTo(c3)
}
```
### Contributing
Contributions are welcome!
If you find any bugs or have suggestions for improvements, please open an issue or submit a pull request on GitHub.