Welcome to mirror list, hosted at ThFree Co, Russian Federation.

memstore.go « cache « gitlab « source « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f53f372f182f0ff2160a1781bc5da90ace5af475 (plain)
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
package cache

import (
	"sync"
	"time"

	cache "github.com/patrickmn/go-cache"
)

type memstore struct {
	store *cache.Cache
	mux   *sync.Mutex
}

var expiration = 10 * time.Minute

func newMemStore() Store {
	return &memstore{
		store: cache.New(expiration, time.Minute),
		mux:   &sync.Mutex{},
	}
}

func (m *memstore) LoadOrCreate(domain string) *Entry {
	m.mux.Lock()
	defer m.mux.Unlock()

	if entry, exists := m.store.Get(domain); exists {
		return entry.(*Entry)
	}

	entry := newCacheEntry(domain)
	m.store.SetDefault(domain, entry)

	return entry
}

func (m *memstore) ReplaceOrCreate(domain string, entry *Entry) *Entry {
	m.mux.Lock()
	defer m.mux.Unlock()

	if _, exists := m.store.Get(domain); exists {
		m.store.Delete(domain)
	}

	m.store.SetDefault(domain, entry)

	return entry
}