Updated documentation
This commit is contained in:
parent
d410df2d7e
commit
06320cf976
52
README.md
52
README.md
@ -1,17 +1,6 @@
|
|||||||
# ocean
|
# ocean
|
||||||
|
|
||||||
In-memory key value store that saves your data in JSON format.
|
In-memory key value store that saves your data to disk using JSON.
|
||||||
|
|
||||||
```
|
|
||||||
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
|
## Installation
|
||||||
|
|
||||||
@ -22,16 +11,16 @@ go get git.akyoto.dev/go/ocean
|
|||||||
## Example
|
## Example
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// User data
|
// User type
|
||||||
type User struct {
|
type User struct { Name string }
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load existing data from ~/.ocean/User.dat
|
// Create a new collection
|
||||||
users := ocean.New[User]()
|
users := ocean.New[User]("todolist")
|
||||||
|
|
||||||
// Store in memory and also store in ~/.ocean/User.dat
|
// Store some data
|
||||||
users.Set("1", &User{Name: "User 1"})
|
users.Set("1", &User{Name: "User 1"})
|
||||||
|
users.Set("2", &User{Name: "User 2"})
|
||||||
|
users.Set("3", &User{Name: "User 3"})
|
||||||
|
|
||||||
// Read from memory
|
// Read from memory
|
||||||
first, err := users.Get("1")
|
first, err := users.Get("1")
|
||||||
@ -42,17 +31,22 @@ for user := range users.All() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In a real project you would usually prefix your collections with a project or company name:
|
Data will be stored in `~/.ocean/todolist/User.dat`.
|
||||||
|
|
||||||
```go
|
## Example file: User.dat
|
||||||
// Data saved to ~/.ocean/google/User.dat
|
|
||||||
users := ocean.New[User]("google")
|
```
|
||||||
|
1
|
||||||
|
{"name":"User 1"}
|
||||||
|
2
|
||||||
|
{"name":"User 2"}
|
||||||
|
3
|
||||||
|
{"name":"User 3"}
|
||||||
```
|
```
|
||||||
|
|
||||||
Disk writes are async and they work like this:
|
## Usage
|
||||||
|
|
||||||
1. Set key and value in memory (sync.Map.Store)
|
1. Create all the collections you need at the start
|
||||||
2. Mark the collection as "dirty" (atomic.StoreUint32)
|
2. `defer users.Sync()` to ensure queued writes will be handled on exit
|
||||||
3. Immediately return control to the program
|
3. Start your web server
|
||||||
|
4. Retrieve and update your data using `Get` and `Set` calls
|
||||||
Because a `Set` call doesn't immediately flush the memory to disk, calling `Set` multiple times in a web server request becomes extremely efficient.
|
|
||||||
|
Loading…
Reference in New Issue
Block a user