Updated documentation
This commit is contained in:
parent
d410df2d7e
commit
06320cf976
52
README.md
52
README.md
@ -1,17 +1,6 @@
|
||||
# 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.
|
||||
In-memory key value store that saves your data to disk using JSON.
|
||||
|
||||
## Installation
|
||||
|
||||
@ -22,16 +11,16 @@ go get git.akyoto.dev/go/ocean
|
||||
## Example
|
||||
|
||||
```go
|
||||
// User data
|
||||
type User struct {
|
||||
Name string
|
||||
}
|
||||
// User type
|
||||
type User struct { Name string }
|
||||
|
||||
// Load existing data from ~/.ocean/User.dat
|
||||
users := ocean.New[User]()
|
||||
// Create a new collection
|
||||
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("2", &User{Name: "User 2"})
|
||||
users.Set("3", &User{Name: "User 3"})
|
||||
|
||||
// Read from memory
|
||||
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
|
||||
// Data saved to ~/.ocean/google/User.dat
|
||||
users := ocean.New[User]("google")
|
||||
## Example file: User.dat
|
||||
|
||||
```
|
||||
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)
|
||||
2. Mark the collection as "dirty" (atomic.StoreUint32)
|
||||
3. 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.
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user