forked from mattrobenolt/go-memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
48 lines (41 loc) · 1.19 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package memcached
import (
"fmt"
"time"
)
// The maximum time to send from a client before
// the timestamp is considered an absolute unix
// timestamp.
const MAX_EXPTIME = 60 * 60 * 24 * 30 // 30 days
type Item struct {
Key string
Value []byte
Flags, Ttl int
Expires time.Time
}
// Check if an Item is expired based on it's Ttl.
// If an item has no Ttl set, it is considered to never
// be expired.
func (i *Item) IsExpired() bool {
return !i.Expires.IsZero() && i.Expires.Before(time.Now())
}
// Set the Ttl and Expires based on the exptime send from
// a client. This follows standard memcached rules, and an
// exptime greater than 30 days is treated as an absolute
// unix timestamp.
func (i *Item) SetExpires(exptime int64) {
if exptime > MAX_EXPTIME {
i.Expires = time.Unix(exptime, 0)
i.Ttl = int(i.Expires.Sub(time.Now()).Seconds())
} else if exptime > 0 {
i.Ttl = int(exptime)
i.Expires = time.Now().Add(time.Duration(exptime) * time.Second)
}
}
func (i *Item) String() string {
return fmt.Sprintf("<Item %s Flags:%d Length:%d Ttl:%d Expires:%s>", i.Key, i.Flags, len(i.Value), i.Ttl, i.Expires)
}
// Initialize a new Item
func NewItem() *Item {
return &Item{}
}