59 lines
1.2 KiB
Markdown
Raw Normal View History

2023-07-04 21:35:20 +00:00
# ocean
2023-07-08 15:26:36 +00:00
In-memory key value store that saves your data in JSON format.
```
1
{"name":"User 1"}
2
{"name":"User 2"}
3
{"name":"User 3"}
```
2023-07-05 20:07:43 +00:00
If you like, you can operate on your entire data with classic UNIX tools.
2023-07-06 12:41:36 +00:00
## Installation
2023-07-06 12:34:22 +00:00
2023-07-06 12:42:25 +00:00
```shell
2023-07-06 12:41:36 +00:00
go get git.akyoto.dev/go/ocean
2023-07-06 12:34:22 +00:00
```
2023-07-06 12:31:23 +00:00
2023-07-06 12:48:29 +00:00
## Example
2023-07-06 12:34:22 +00:00
```go
2023-07-06 12:41:36 +00:00
// User data
type User struct {
Name string
}
2023-07-08 15:26:36 +00:00
// Load existing data from ~/.ocean/User.dat
2023-07-06 13:02:50 +00:00
users := ocean.New[User]()
2023-07-06 12:31:23 +00:00
2023-07-08 15:26:36 +00:00
// Store in memory and also store in ~/.ocean/User.dat
2023-07-06 12:10:12 +00:00
users.Set("1", &User{Name: "User 1"})
2023-07-06 12:48:29 +00:00
2023-07-06 13:02:50 +00:00
// Read from memory
2023-07-08 15:26:36 +00:00
first, err := users.Get("1")
2023-07-06 13:15:51 +00:00
// Iterate over all users
for user := range users.All() {
fmt.Println(user.Name)
}
2023-07-06 12:10:12 +00:00
```
2023-07-06 12:27:48 +00:00
2023-07-06 12:34:22 +00:00
In a real project you would usually prefix your collections with a project or company name:
2023-07-06 12:27:48 +00:00
```go
2023-07-08 15:26:36 +00:00
// Data saved to ~/.ocean/google/User.dat
2023-07-06 13:02:50 +00:00
users := ocean.New[User]("google")
2023-07-06 12:27:48 +00:00
```
2023-07-08 15:26:36 +00:00
Disk writes are async and they work like this:
2023-07-06 13:12:29 +00:00
2023-07-08 15:26:36 +00:00
1. Set key and value in memory (sync.Map.Store)
2. Mark the collection as "dirty" (atomic.StoreUint32)
3. Immediately return control to the program
2023-07-06 14:51:38 +00:00
2023-07-08 15:26:36 +00:00
Because a `Set` call doesn't immediately flush the memory to disk, calling `Set` multiple times in a web server request becomes extremely efficient.