cubefs

Форк
0
/
dcache.go 
72 строки · 1.7 Кб
1
// Copyright 2018 The CubeFS Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package fs
16

17
import (
18
	"sync"
19
	"time"
20
)
21

22
// DentryCache defines the dentry cache.
23
type DentryCache struct {
24
	sync.Mutex
25
	cache      map[string]uint64
26
	expiration time.Time
27
}
28

29
// NewDentryCache returns a new dentry cache.
30
func NewDentryCache() *DentryCache {
31
	return &DentryCache{
32
		cache:      make(map[string]uint64),
33
		expiration: time.Now().Add(DentryValidDuration),
34
	}
35
}
36

37
// Put puts an item into the cache.
38
func (dc *DentryCache) Put(name string, ino uint64) {
39
	if dc == nil {
40
		return
41
	}
42
	dc.Lock()
43
	defer dc.Unlock()
44
	dc.cache[name] = ino
45
	dc.expiration = time.Now().Add(DentryValidDuration)
46
}
47

48
// Get gets the item from the cache based on the given key.
49
func (dc *DentryCache) Get(name string) (uint64, bool) {
50
	if dc == nil {
51
		return 0, false
52
	}
53

54
	dc.Lock()
55
	defer dc.Unlock()
56
	if dc.expiration.Before(time.Now()) {
57
		dc.cache = make(map[string]uint64)
58
		return 0, false
59
	}
60
	ino, ok := dc.cache[name]
61
	return ino, ok
62
}
63

64
// Delete deletes the item based on the given key.
65
func (dc *DentryCache) Delete(name string) {
66
	if dc == nil {
67
		return
68
	}
69
	dc.Lock()
70
	defer dc.Unlock()
71
	delete(dc.cache, name)
72
}
73

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.