/usr/share/gocode/src/github.com/odeke-em/cache/cache.go is in golang-github-odeke-em-cache-dev 0.0~git20151107.0.baf8e436-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | package expirable
import (
"sync"
"time"
)
type Operation int
const (
Noop Operation = 1 << iota
Create
Read
Update
Delete
)
type Keyable interface{}
type Expirable interface {
Expired(baseTime time.Time) bool
Value() interface{}
}
type KeyValue struct {
Key Keyable
Value Expirable
}
type OperationCache struct {
mu *sync.Mutex
body map[Keyable]Expirable
}
func New() *OperationCache {
return &OperationCache{
mu: &sync.Mutex{},
body: make(map[Keyable]Expirable),
}
}
func (oc *OperationCache) Put(k Keyable, v Expirable) bool {
oc.mu.Lock()
defer oc.mu.Unlock()
oc.body[k] = v
return true
}
func (oc *OperationCache) Get(k Keyable) (Expirable, bool) {
oc.mu.Lock()
v, ok := oc.body[k]
oc.mu.Unlock()
if ok && v != nil && v.Expired(time.Now()) {
prev, _ := oc.Remove(k)
return prev, false
}
return v, ok
}
func (oc *OperationCache) Remove(k Keyable) (prev Expirable, exist bool) {
oc.mu.Lock()
defer oc.mu.Unlock()
prev, exist = oc.body[k]
if exist {
delete(oc.body, k)
}
return prev, exist
}
func (oc *OperationCache) Items() chan *KeyValue {
items := make(chan *KeyValue)
go func() {
defer close(items)
for k, v := range oc.body {
items <- &KeyValue{Key: k, Value: v}
}
}()
return items
}
|