File: bucket.go

package info (click to toggle)
golang-github-karlseguin-ccache 2.0.2%2Bgit20161222.2.12c7ffd-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 132 kB
  • ctags: 161
  • sloc: makefile: 6
file content (41 lines) | stat: -rw-r--r-- 711 bytes parent folder | download
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
package ccache

import (
	"sync"
	"time"
)

type bucket struct {
	sync.RWMutex
	lookup map[string]*Item
}

func (b *bucket) get(key string) *Item {
	b.RLock()
	defer b.RUnlock()
	return b.lookup[key]
}

func (b *bucket) set(key string, value interface{}, duration time.Duration) (*Item, *Item) {
	expires := time.Now().Add(duration).UnixNano()
	item := newItem(key, value, expires)
	b.Lock()
	defer b.Unlock()
	existing := b.lookup[key]
	b.lookup[key] = item
	return item, existing
}

func (b *bucket) delete(key string) *Item {
	b.Lock()
	defer b.Unlock()
	item := b.lookup[key]
	delete(b.lookup, key)
	return item
}

func (b *bucket) clear() {
	b.Lock()
	defer b.Unlock()
	b.lookup = make(map[string]*Item)
}