/
cache_entry.go
58 lines (47 loc) · 1.29 KB
/
cache_entry.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
// Copyright Chainify Group LTD. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package materials
import (
"time"
"github.com/chainifynet/aws-encryption-sdk-go/pkg/model"
)
// CacheEntry CachingCryptoMaterialsManager still under development.
type CacheEntry[V model.EncryptionMaterials | model.DecryptionMaterials] struct {
key []byte
value V
createdAt time.Time
lifetime time.Duration
messages uint64
bytes int
valid bool
}
// NewCacheEntry CachingCryptoMaterialsManager still under development.
func NewCacheEntry[V model.EncryptionMaterials | model.DecryptionMaterials](key []byte, value V, lifetime time.Duration) *CacheEntry[V] {
return &CacheEntry[V]{
key: key,
value: value,
createdAt: time.Now(),
lifetime: lifetime,
valid: true,
}
}
func (ce *CacheEntry[V]) Key() []byte {
return ce.key
}
func (ce *CacheEntry[V]) Value() V {
return ce.value
}
func (ce *CacheEntry[V]) Age() float64 {
return time.Since(ce.createdAt).Seconds()
//return time.Now().Sub(ce.createdAt).Seconds()
}
func (ce *CacheEntry[V]) IsTooOld() bool {
return ce.Age() > ce.lifetime.Seconds()
}
func (ce *CacheEntry[V]) updateMeta(b []byte) {
ce.bytes += len(b)
ce.messages++
}
func (ce *CacheEntry[V]) invalidate() {
ce.valid = false
}