2023-07-04 21:35:20 +00:00
|
|
|
# ocean
|
|
|
|
|
2023-07-08 17:28:57 +00:00
|
|
|
In-memory key value store that saves your data to disk using JSON.
|
2023-07-05 20:07:43 +00:00
|
|
|
|
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-08 17:28:57 +00:00
|
|
|
// User type
|
|
|
|
type User struct { Name string }
|
2023-07-06 12:41:36 +00:00
|
|
|
|
2023-07-08 17:28:57 +00:00
|
|
|
// Create a new collection
|
2023-07-12 08:58:20 +00:00
|
|
|
todolist := ocean.New("todolist")
|
|
|
|
users := todolist.NewCollection[User](&storage.File[User]{})
|
2023-07-06 12:31:23 +00:00
|
|
|
|
2023-07-08 17:28:57 +00:00
|
|
|
// Store some data
|
2023-07-06 12:10:12 +00:00
|
|
|
users.Set("1", &User{Name: "User 1"})
|
2023-07-08 17:28:57 +00:00
|
|
|
users.Set("2", &User{Name: "User 2"})
|
|
|
|
users.Set("3", &User{Name: "User 3"})
|
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-08 17:28:57 +00:00
|
|
|
Data will be stored in `~/.ocean/todolist/User.dat`.
|
2023-07-06 12:27:48 +00:00
|
|
|
|
2023-07-10 14:27:41 +00:00
|
|
|
## Format
|
2023-07-06 12:27:48 +00:00
|
|
|
|
2023-07-08 20:10:02 +00:00
|
|
|
```json
|
2023-07-08 17:28:57 +00:00
|
|
|
1
|
|
|
|
{"name":"User 1"}
|
|
|
|
2
|
|
|
|
{"name":"User 2"}
|
|
|
|
3
|
|
|
|
{"name":"User 3"}
|
|
|
|
```
|
2023-07-06 13:12:29 +00:00
|
|
|
|
2023-07-12 08:58:20 +00:00
|
|
|
## Benchmarks
|
|
|
|
|
|
|
|
```
|
|
|
|
BenchmarkGet-12 275126157 4.462 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkSet-12 4796011 251.0 ns/op 32 B/op 2 allocs/op
|
|
|
|
BenchmarkDelete-12 471913158 2.530 ns/op 0 B/op 0 allocs/op
|
|
|
|
BenchmarkNew-12 48838576 22.89 ns/op 80 B/op 1 allocs/op
|
|
|
|
```
|
|
|
|
|
2023-07-08 17:28:57 +00:00
|
|
|
## Usage
|
2023-07-06 14:51:38 +00:00
|
|
|
|
2023-07-08 17:28:57 +00:00
|
|
|
1. Create all the collections you need at the start
|
|
|
|
2. `defer users.Sync()` to ensure queued writes will be handled on exit
|
|
|
|
3. Start your web server
|
|
|
|
4. Retrieve and update your data using `Get` and `Set` calls
|