/
item.option.go
61 lines (51 loc) · 1.45 KB
/
item.option.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
49
50
51
52
53
54
55
56
57
58
59
60
61
package xzmapitem
import "github.com/golang-module/carbon/v2"
type Option[T any] func(item *item[T])
// 设置删除函数
func WithDeleteHandler[T any](fn func(v T)) Option[T] {
return func(item *item[T]) {
item.deleteHandler = fn
}
}
// 设置过期时间
func WithExpiration[T any](expiration int64) Option[T] {
return func(item *item[T]) {
item.expiration = expiration
}
}
// 无过期时间
func WithNoExpiration[T any]() Option[T] {
return func(item *item[T]) {
item.expiration = 0
}
}
// 设置过期时间(秒)
func WithDurationSeconds[T any](seconds int) Option[T] {
return func(item *item[T]) {
item.expiration = carbon.Now().AddSeconds(seconds).Carbon2Time().UnixNano()
}
}
// 设置过期时间(分钟)
func WithDurationMinutes[T any](minutes int) Option[T] {
return func(item *item[T]) {
item.expiration = carbon.Now().AddMinutes(minutes).Carbon2Time().UnixNano()
}
}
// 设置过期时间(小时)
func WithDurationHours[T any](hours int) Option[T] {
return func(item *item[T]) {
item.expiration = carbon.Now().AddHours(hours).Carbon2Time().UnixNano()
}
}
// 设置过期时间(天)
func WithDurationDays[T any](days int) Option[T] {
return func(item *item[T]) {
item.expiration = carbon.Now().AddDays(days).Carbon2Time().UnixNano()
}
}
// 设置过期时间(月份)
func WithDurationMonths[T any](months int) Option[T] {
return func(item *item[T]) {
item.expiration = carbon.Now().AddMonths(months).Carbon2Time().UnixNano()
}
}