💿 In-memory key value store that saves your data to disk using JSON.
32 Commits
.editorconfig | ||
.gitignore | ||
Collection_test.go | ||
Collection.go | ||
Decoder.go | ||
DirectoryStorage.go | ||
Encoder.go | ||
Errors.go | ||
FileStorage.go | ||
go.mod | ||
go.sum | ||
keyValue.go | ||
LICENSE | ||
README.md | ||
Storage.go |
ocean
In-memory key value store that saves your data in JSON format.
1
{"name":"User 1"}
2
{"name":"User 2"}
3
{"name":"User 3"}
If you like, you can operate on your entire data with classic UNIX tools.
Installation
go get git.akyoto.dev/go/ocean
Example
// User data
type User struct {
Name string
}
// Load existing data from ~/.ocean/User.dat
users := ocean.New[User]()
// Store in memory and also store in ~/.ocean/User.dat
users.Set("1", &User{Name: "User 1"})
// Read from memory
first, err := users.Get("1")
// Iterate over all users
for user := range users.All() {
fmt.Println(user.Name)
}
In a real project you would usually prefix your collections with a project or company name:
// Data saved to ~/.ocean/google/User.dat
users := ocean.New[User]("google")
Disk writes are async and they work like this:
- Set key and value in memory (sync.Map.Store)
- Mark the collection as "dirty" (atomic.StoreUint32)
- Immediately return control to the program
Because a Set
call doesn't immediately flush the memory to disk, calling Set
multiple times in a web server request becomes extremely efficient.