mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-11 03:13:31 +00:00
feat(core): replace increment with get & set
This commit is contained in:
1 parent
c07c6141ba
commit
12b5307a71
1 file changed
+12
-5
+12
-5
@@ -116,13 +116,20 @@ func main() {
|
|||||||
|
|
||||||
func incrementCounter(col *gocb.Collection) error {
|
func incrementCounter(col *gocb.Collection) error {
|
||||||
counterDocId := "counter"
|
counterDocId := "counter"
|
||||||
// Increment by 1, creating doc if needed.
|
docOut, err := col.Get(counterDocId, &gocb.GetOptions{})
|
||||||
// By using `Initial: 1` we set the starting count(non-negative) to 1 if the document needs to be created.
|
|
||||||
// If it already exists, the count will increase by the amount provided in the Delta option(i.e 1).
|
|
||||||
increment, err := col.Binary().Increment(counterDocId, &gocb.IncrementOptions{Initial: 1, Delta: 1})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Printf("Counter : %d\n", increment.Content())
|
var current uint64
|
||||||
|
err = docOut.Content(¤t)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
current++
|
||||||
|
_, err = col.Upsert(counterDocId, current, &gocb.UpsertOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Printf("Counter : %d\n", current)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user