@@ -3,6 +3,7 @@ package polycode
3
3
import (
4
4
"fmt"
5
5
"reflect"
6
+ "time"
6
7
)
7
8
8
9
type DataStore struct {
@@ -27,6 +28,17 @@ type Collection struct {
27
28
}
28
29
29
30
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
+
30
42
id , err := GetId (item )
31
43
if err != nil {
32
44
fmt .Printf ("failed to get id: %s\n " , err .Error ())
@@ -38,6 +50,7 @@ func (c Collection) InsertOne(item interface{}) error {
38
50
Collection : c .name ,
39
51
Key : id ,
40
52
Item : item ,
53
+ TTL : ttl ,
41
54
}
42
55
43
56
if c .isGlobal {
@@ -54,6 +67,17 @@ func (c Collection) InsertOne(item interface{}) error {
54
67
}
55
68
56
69
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
+
57
81
id , err := GetId (item )
58
82
if err != nil {
59
83
fmt .Printf ("failed to get id: %s\n " , err .Error ())
@@ -65,6 +89,7 @@ func (c Collection) UpdateOne(item interface{}) error {
65
89
Collection : c .name ,
66
90
Key : id ,
67
91
Item : item ,
92
+ TTL : ttl ,
68
93
}
69
94
70
95
if c .isGlobal {
@@ -81,6 +106,17 @@ func (c Collection) UpdateOne(item interface{}) error {
81
106
}
82
107
83
108
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
+
84
120
id , err := GetId (item )
85
121
if err != nil {
86
122
fmt .Printf ("failed to get id: %s\n " , err .Error ())
@@ -92,6 +128,7 @@ func (c Collection) UpsertOne(item interface{}) error {
92
128
Collection : c .name ,
93
129
Key : id ,
94
130
Item : item ,
131
+ TTL : ttl ,
95
132
}
96
133
97
134
if c .isGlobal {
0 commit comments