-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
89 lines (72 loc) · 2.61 KB
/
cache.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
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
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lrucache
import (
"reflect"
"unsafe"
)
type DeleteCallback func(key []byte, entry interface{})
type MergeOperator func(old_entry, new_entry interface{}) interface{}
type ChargeOperator func(entry interface{}, old_charge, new_charge uint64) uint64
type TravelEntryOperator func(key []byte, entry interface{})
type Cache interface {
Put(key string, value string)
Get(key string) (string, bool)
Delete(key string)
NewId() uint64
Prune()
TotalCharge() uint64
Insert(key[]byte, entry interface{}, charge uint64, deleter DeleteCallback)
Lookup(key []byte) interface{}
Remove(key []byte) interface{}
Merge(key []byte, entry interface{}, charge uint64, merge_opt MergeOperator, charge_opt ChargeOperator) (old_entry interface{})
ApplyToAllCacheEntries(TravelEntryOperator)
}
var IntMergeOperator MergeOperator = func(old_entry, new_entry interface{}) interface{} {
if old_entry == nil {
return new_entry
}
old, ok_old := old_entry.(int)
new, ok_new := new_entry.(int)
if !ok_old || !ok_new {
panic("error of merge type, old:" + reflect.TypeOf(old_entry).Name() +
" new:" + reflect.TypeOf(new_entry).Name())
}
res := old + new
return res
}
var IntChargeOperator ChargeOperator = func(entry interface{}, old_charge, new_charge uint64) uint64 {
var a int
return uint64(unsafe.Sizeof(a))
}
var Int64MergeOperator MergeOperator = func(old_entry, new_entry interface{}) interface{} {
if old_entry == nil {
return new_entry
}
old, ok_old := old_entry.(int64)
new, ok_new := new_entry.(int64)
if !ok_old || !ok_new {
panic("error of merge type, old:" + reflect.TypeOf(old_entry).Name() +
" new:" + reflect.TypeOf(new_entry).Name())
}
res := old + new
return res
}
var Int64ChargeOperator ChargeOperator = func(entry interface{}, old_charge, new_charge uint64) uint64 {
var a int64
return uint64(unsafe.Sizeof(a))
}