Skip to content

Commit 38cb753

Browse files
committed

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

polycode/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type PutRequest struct {
9494
Collection string `json:"collection"`
9595
Key string `json:"key"`
9696
Item any `json:"item"`
97-
Counter *Counter `json:"counter.,omitempty"`
97+
TTL int64 `json:"TTL"`
9898
}
9999

100100
type Counter struct {

polycode/datastore.go

+37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package polycode
33
import (
44
"fmt"
55
"reflect"
6+
"time"
67
)
78

89
type DataStore struct {
@@ -27,6 +28,17 @@ type Collection struct {
2728
}
2829

2930
func (c Collection) InsertOne(item interface{}) error {
31+
return c.InsertOneWithTTL(item, -1)
32+
}
33+
34+
func (c Collection) InsertOneWithTTL(item interface{}, expireIn time.Duration) error {
35+
var ttl int64
36+
if expireIn == -1 {
37+
ttl = -1
38+
} else {
39+
ttl = time.Now().Unix() + int64(expireIn.Seconds())
40+
}
41+
3042
id, err := GetId(item)
3143
if err != nil {
3244
fmt.Printf("failed to get id: %s\n", err.Error())
@@ -38,6 +50,7 @@ func (c Collection) InsertOne(item interface{}) error {
3850
Collection: c.name,
3951
Key: id,
4052
Item: item,
53+
TTL: ttl,
4154
}
4255

4356
if c.isGlobal {
@@ -54,6 +67,17 @@ func (c Collection) InsertOne(item interface{}) error {
5467
}
5568

5669
func (c Collection) UpdateOne(item interface{}) error {
70+
return c.UpdateOneWithTTL(item, -1)
71+
}
72+
73+
func (c Collection) UpdateOneWithTTL(item interface{}, expireIn time.Duration) error {
74+
var ttl int64
75+
if expireIn == -1 {
76+
ttl = -1
77+
} else {
78+
ttl = time.Now().Unix() + int64(expireIn.Seconds())
79+
}
80+
5781
id, err := GetId(item)
5882
if err != nil {
5983
fmt.Printf("failed to get id: %s\n", err.Error())
@@ -65,6 +89,7 @@ func (c Collection) UpdateOne(item interface{}) error {
6589
Collection: c.name,
6690
Key: id,
6791
Item: item,
92+
TTL: ttl,
6893
}
6994

7095
if c.isGlobal {
@@ -81,6 +106,17 @@ func (c Collection) UpdateOne(item interface{}) error {
81106
}
82107

83108
func (c Collection) UpsertOne(item interface{}) error {
109+
return c.UpsertOneWithTTL(item, -1)
110+
}
111+
112+
func (c Collection) UpsertOneWithTTL(item interface{}, expireIn time.Duration) error {
113+
var ttl int64
114+
if expireIn == -1 {
115+
ttl = -1
116+
} else {
117+
ttl = time.Now().Unix() + int64(expireIn.Seconds())
118+
}
119+
84120
id, err := GetId(item)
85121
if err != nil {
86122
fmt.Printf("failed to get id: %s\n", err.Error())
@@ -92,6 +128,7 @@ func (c Collection) UpsertOne(item interface{}) error {
92128
Collection: c.name,
93129
Key: id,
94130
Item: item,
131+
TTL: ttl,
95132
}
96133

97134
if c.isGlobal {

0 commit comments

Comments
 (0)