@@ -10,14 +10,16 @@ import (
10
10
"time"
11
11
)
12
12
13
+ var emptyTime = time.Time {}
14
+
13
15
type Item struct {
14
16
Object interface {}
15
- Expiration * time.Time
17
+ Expiration time.Time
16
18
}
17
19
18
20
// Returns true if the item has expired.
19
- func (item * Item ) Expired () bool {
20
- if item .Expiration == nil {
21
+ func (item Item ) Expired () bool {
22
+ if item .Expiration == emptyTime {
21
23
return false
22
24
}
23
25
return item .Expiration .Before (time .Now ())
@@ -39,7 +41,7 @@ type Cache struct {
39
41
40
42
type cache struct {
41
43
defaultExpiration time.Duration
42
- items map [string ]* Item
44
+ items map [string ]Item
43
45
mu sync.RWMutex
44
46
onEvicted func (string , interface {})
45
47
janitor * janitor
@@ -57,15 +59,14 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
57
59
}
58
60
59
61
func (c * cache ) set (k string , x interface {}, d time.Duration ) {
60
- var e * time. Time
62
+ e := emptyTime
61
63
if d == DefaultExpiration {
62
64
d = c .defaultExpiration
63
65
}
64
66
if d > 0 {
65
- t := time .Now ().Add (d )
66
- e = & t
67
+ e = time .Now ().Add (d )
67
68
}
68
- c .items [k ] = & Item {
69
+ c .items [k ] = Item {
69
70
Object : x ,
70
71
Expiration : e ,
71
72
}
@@ -159,6 +160,7 @@ func (c *cache) Increment(k string, n int64) error {
159
160
c .mu .Unlock ()
160
161
return fmt .Errorf ("The value for %s is not an integer" , k )
161
162
}
163
+ c .items [k ] = v
162
164
c .mu .Unlock ()
163
165
return nil
164
166
}
@@ -184,6 +186,7 @@ func (c *cache) IncrementFloat(k string, n float64) error {
184
186
c .mu .Unlock ()
185
187
return fmt .Errorf ("The value for %s does not have type float32 or float64" , k )
186
188
}
189
+ c .items [k ] = v
187
190
c .mu .Unlock ()
188
191
return nil
189
192
}
@@ -205,6 +208,7 @@ func (c *cache) IncrementInt(k string, n int) (int, error) {
205
208
}
206
209
nv := rv + n
207
210
v .Object = nv
211
+ c .items [k ] = v
208
212
c .mu .Unlock ()
209
213
return nv , nil
210
214
}
@@ -226,6 +230,7 @@ func (c *cache) IncrementInt8(k string, n int8) (int8, error) {
226
230
}
227
231
nv := rv + n
228
232
v .Object = nv
233
+ c .items [k ] = v
229
234
c .mu .Unlock ()
230
235
return nv , nil
231
236
}
@@ -247,6 +252,7 @@ func (c *cache) IncrementInt16(k string, n int16) (int16, error) {
247
252
}
248
253
nv := rv + n
249
254
v .Object = nv
255
+ c .items [k ] = v
250
256
c .mu .Unlock ()
251
257
return nv , nil
252
258
}
@@ -268,6 +274,7 @@ func (c *cache) IncrementInt32(k string, n int32) (int32, error) {
268
274
}
269
275
nv := rv + n
270
276
v .Object = nv
277
+ c .items [k ] = v
271
278
c .mu .Unlock ()
272
279
return nv , nil
273
280
}
@@ -289,6 +296,7 @@ func (c *cache) IncrementInt64(k string, n int64) (int64, error) {
289
296
}
290
297
nv := rv + n
291
298
v .Object = nv
299
+ c .items [k ] = v
292
300
c .mu .Unlock ()
293
301
return nv , nil
294
302
}
@@ -310,6 +318,7 @@ func (c *cache) IncrementUint(k string, n uint) (uint, error) {
310
318
}
311
319
nv := rv + n
312
320
v .Object = nv
321
+ c .items [k ] = v
313
322
c .mu .Unlock ()
314
323
return nv , nil
315
324
}
@@ -331,6 +340,7 @@ func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) {
331
340
}
332
341
nv := rv + n
333
342
v .Object = nv
343
+ c .items [k ] = v
334
344
c .mu .Unlock ()
335
345
return nv , nil
336
346
}
@@ -352,6 +362,7 @@ func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) {
352
362
}
353
363
nv := rv + n
354
364
v .Object = nv
365
+ c .items [k ] = v
355
366
c .mu .Unlock ()
356
367
return nv , nil
357
368
}
@@ -373,6 +384,7 @@ func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) {
373
384
}
374
385
nv := rv + n
375
386
v .Object = nv
387
+ c .items [k ] = v
376
388
c .mu .Unlock ()
377
389
return nv , nil
378
390
}
@@ -394,6 +406,7 @@ func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) {
394
406
}
395
407
nv := rv + n
396
408
v .Object = nv
409
+ c .items [k ] = v
397
410
c .mu .Unlock ()
398
411
return nv , nil
399
412
}
@@ -415,6 +428,7 @@ func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) {
415
428
}
416
429
nv := rv + n
417
430
v .Object = nv
431
+ c .items [k ] = v
418
432
c .mu .Unlock ()
419
433
return nv , nil
420
434
}
@@ -436,6 +450,7 @@ func (c *cache) IncrementFloat32(k string, n float32) (float32, error) {
436
450
}
437
451
nv := rv + n
438
452
v .Object = nv
453
+ c .items [k ] = v
439
454
c .mu .Unlock ()
440
455
return nv , nil
441
456
}
@@ -457,6 +472,7 @@ func (c *cache) IncrementFloat64(k string, n float64) (float64, error) {
457
472
}
458
473
nv := rv + n
459
474
v .Object = nv
475
+ c .items [k ] = v
460
476
c .mu .Unlock ()
461
477
return nv , nil
462
478
}
@@ -506,6 +522,7 @@ func (c *cache) Decrement(k string, n int64) error {
506
522
c .mu .Unlock ()
507
523
return fmt .Errorf ("The value for %s is not an integer" , k )
508
524
}
525
+ c .items [k ] = v
509
526
c .mu .Unlock ()
510
527
return nil
511
528
}
@@ -531,6 +548,7 @@ func (c *cache) DecrementFloat(k string, n float64) error {
531
548
c .mu .Unlock ()
532
549
return fmt .Errorf ("The value for %s does not have type float32 or float64" , k )
533
550
}
551
+ c .items [k ] = v
534
552
c .mu .Unlock ()
535
553
return nil
536
554
}
@@ -552,6 +570,7 @@ func (c *cache) DecrementInt(k string, n int) (int, error) {
552
570
}
553
571
nv := rv - n
554
572
v .Object = nv
573
+ c .items [k ] = v
555
574
c .mu .Unlock ()
556
575
return nv , nil
557
576
}
@@ -573,6 +592,7 @@ func (c *cache) DecrementInt8(k string, n int8) (int8, error) {
573
592
}
574
593
nv := rv - n
575
594
v .Object = nv
595
+ c .items [k ] = v
576
596
c .mu .Unlock ()
577
597
return nv , nil
578
598
}
@@ -594,6 +614,7 @@ func (c *cache) DecrementInt16(k string, n int16) (int16, error) {
594
614
}
595
615
nv := rv - n
596
616
v .Object = nv
617
+ c .items [k ] = v
597
618
c .mu .Unlock ()
598
619
return nv , nil
599
620
}
@@ -615,6 +636,7 @@ func (c *cache) DecrementInt32(k string, n int32) (int32, error) {
615
636
}
616
637
nv := rv - n
617
638
v .Object = nv
639
+ c .items [k ] = v
618
640
c .mu .Unlock ()
619
641
return nv , nil
620
642
}
@@ -636,6 +658,7 @@ func (c *cache) DecrementInt64(k string, n int64) (int64, error) {
636
658
}
637
659
nv := rv - n
638
660
v .Object = nv
661
+ c .items [k ] = v
639
662
c .mu .Unlock ()
640
663
return nv , nil
641
664
}
@@ -657,6 +680,7 @@ func (c *cache) DecrementUint(k string, n uint) (uint, error) {
657
680
}
658
681
nv := rv - n
659
682
v .Object = nv
683
+ c .items [k ] = v
660
684
c .mu .Unlock ()
661
685
return nv , nil
662
686
}
@@ -678,6 +702,7 @@ func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) {
678
702
}
679
703
nv := rv - n
680
704
v .Object = nv
705
+ c .items [k ] = v
681
706
c .mu .Unlock ()
682
707
return nv , nil
683
708
}
@@ -699,6 +724,7 @@ func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) {
699
724
}
700
725
nv := rv - n
701
726
v .Object = nv
727
+ c .items [k ] = v
702
728
c .mu .Unlock ()
703
729
return nv , nil
704
730
}
@@ -720,6 +746,7 @@ func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) {
720
746
}
721
747
nv := rv - n
722
748
v .Object = nv
749
+ c .items [k ] = v
723
750
c .mu .Unlock ()
724
751
return nv , nil
725
752
}
@@ -741,6 +768,7 @@ func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) {
741
768
}
742
769
nv := rv - n
743
770
v .Object = nv
771
+ c .items [k ] = v
744
772
c .mu .Unlock ()
745
773
return nv , nil
746
774
}
@@ -762,6 +790,7 @@ func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) {
762
790
}
763
791
nv := rv - n
764
792
v .Object = nv
793
+ c .items [k ] = v
765
794
c .mu .Unlock ()
766
795
return nv , nil
767
796
}
@@ -783,6 +812,7 @@ func (c *cache) DecrementFloat32(k string, n float32) (float32, error) {
783
812
}
784
813
nv := rv - n
785
814
v .Object = nv
815
+ c .items [k ] = v
786
816
c .mu .Unlock ()
787
817
return nv , nil
788
818
}
@@ -804,6 +834,7 @@ func (c *cache) DecrementFloat64(k string, n float64) (float64, error) {
804
834
}
805
835
nv := rv - n
806
836
v .Object = nv
837
+ c .items [k ] = v
807
838
c .mu .Unlock ()
808
839
return nv , nil
809
840
}
@@ -906,7 +937,7 @@ func (c *cache) SaveFile(fname string) error {
906
937
// documentation for NewFrom().)
907
938
func (c * cache ) Load (r io.Reader ) error {
908
939
dec := gob .NewDecoder (r )
909
- items := map [string ]* Item {}
940
+ items := map [string ]Item {}
910
941
err := dec .Decode (& items )
911
942
if err == nil {
912
943
c .mu .Lock ()
@@ -944,7 +975,7 @@ func (c *cache) LoadFile(fname string) error {
944
975
// fields of the items should be checked. Note that explicit synchronization
945
976
// is needed to use a cache and its corresponding Items() return value at
946
977
// the same time, as the map is shared.
947
- func (c * cache ) Items () map [string ]* Item {
978
+ func (c * cache ) Items () map [string ]Item {
948
979
c .mu .RLock ()
949
980
defer c .mu .RUnlock ()
950
981
return c .items
@@ -962,7 +993,7 @@ func (c *cache) ItemCount() int {
962
993
// Delete all items from the cache.
963
994
func (c * cache ) Flush () {
964
995
c .mu .Lock ()
965
- c .items = map [string ]* Item {}
996
+ c .items = map [string ]Item {}
966
997
c .mu .Unlock ()
967
998
}
968
999
@@ -997,7 +1028,7 @@ func runJanitor(c *cache, ci time.Duration) {
997
1028
go j .Run (c )
998
1029
}
999
1030
1000
- func newCache (de time.Duration , m map [string ]* Item ) * cache {
1031
+ func newCache (de time.Duration , m map [string ]Item ) * cache {
1001
1032
if de == 0 {
1002
1033
de = - 1
1003
1034
}
@@ -1008,7 +1039,7 @@ func newCache(de time.Duration, m map[string]*Item) *cache {
1008
1039
return c
1009
1040
}
1010
1041
1011
- func newCacheWithJanitor (de time.Duration , ci time.Duration , m map [string ]* Item ) * Cache {
1042
+ func newCacheWithJanitor (de time.Duration , ci time.Duration , m map [string ]Item ) * Cache {
1012
1043
c := newCache (de , m )
1013
1044
// This trick ensures that the janitor goroutine (which--granted it
1014
1045
// was enabled--is running DeleteExpired on c forever) does not keep
@@ -1029,7 +1060,7 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item)
1029
1060
// manually. If the cleanup interval is less than one, expired items are not
1030
1061
// deleted from the cache before calling c.DeleteExpired().
1031
1062
func New (defaultExpiration , cleanupInterval time.Duration ) * Cache {
1032
- items := make (map [string ]* Item )
1063
+ items := make (map [string ]Item )
1033
1064
return newCacheWithJanitor (defaultExpiration , cleanupInterval , items )
1034
1065
}
1035
1066
@@ -1042,7 +1073,7 @@ func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
1042
1073
// NewFrom() also accepts an items map which will serve as the underlying map
1043
1074
// for the cache. This is useful for starting from a deserialized cache
1044
1075
// (serialized using e.g. gob.Encode() on c.Items()), or passing in e.g.
1045
- // make(map[string]* Item, 500) to improve startup performance when the cache
1076
+ // make(map[string]Item, 500) to improve startup performance when the cache
1046
1077
// is expected to reach a certain minimum size.
1047
1078
//
1048
1079
// Only the cache's methods synchronize access to this map, so it is not
@@ -1054,6 +1085,6 @@ func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
1054
1085
// gob.Register() the individual types stored in the cache before encoding a
1055
1086
// map retrieved with c.Items(), and to register those same types before
1056
1087
// decoding a blob containing an items map.
1057
- func NewFrom (defaultExpiration , cleanupInterval time.Duration , items map [string ]* Item ) * Cache {
1088
+ func NewFrom (defaultExpiration , cleanupInterval time.Duration , items map [string ]Item ) * Cache {
1058
1089
return newCacheWithJanitor (defaultExpiration , cleanupInterval , items )
1059
1090
}
0 commit comments