https://github.com/nir3x/concurrentcache
Concurrent Cache - Concurrent-Safe Cache for Go
https://github.com/nir3x/concurrentcache
cache concurrency concurrent-cache data-structure go golang
Last synced: 3 months ago
JSON representation
Concurrent Cache - Concurrent-Safe Cache for Go
- Host: GitHub
- URL: https://github.com/nir3x/concurrentcache
- Owner: NIR3X
- License: agpl-3.0
- Created: 2024-01-27T02:57:26.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-16T04:01:47.000Z (over 1 year ago)
- Last Synced: 2024-06-21T17:51:01.917Z (11 months ago)
- Topics: cache, concurrency, concurrent-cache, data-structure, go, golang
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Concurrent Cache - Concurrent-Safe Cache for Go
Concurrent Cache is a Go package providing a concurrent-safe cache with read and write access.
## Features
- Safe concurrent read and write access to a cache.
- Periodic updates to the cache with user-defined logic.## Installation
```bash
go get -u github.com/NIR3X/concurrentcache
```## Usage
```go
package mainimport (
"bytes"
"fmt"
"time""github.com/NIR3X/concurrentcache"
)func main() {
// Create a new concurrent cache with a one-second update interval.
c := concurrentcache.NewConcurrentCache[map[string][]uint8](make(map[string][]uint8), time.Second, func(locker concurrentcache.Locker, cache map[string][]uint8) {
locker.Lock()
defer locker.Unlock()
// Your custom update logic here
})// Close the cache when done to stop the update goroutine.
defer c.Close()// AccessWrite example
c.AccessWrite(func(cache map[string][]uint8) {
cache["key"] = []uint8{1, 2, 3}
})// AccessRead example
c.AccessRead(func(cache map[string][]uint8) {
if value := cache["key"]; !bytes.Equal(value, []uint8{1, 2, 3}) {
fmt.Println("Expected [1, 2, 3] but got", value)
}
})
}
```## License
[](https://www.gnu.org/licenses/agpl-3.0.html)
This program is Free Software: You can use, study share and improve it at your
will. Specifically you can redistribute and/or modify it under the terms of the
[GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html) as
published by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.