mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-17 10:58:49 -07:00
68c1a0b9b4
This solves the erratic test failures on model.TestIgnores by ensuring that the ignore patterns are reloaded even in the face of unchanged timestamps. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4208
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package ignore
|
|
|
|
import "time"
|
|
|
|
type nower interface {
|
|
Now() time.Time
|
|
}
|
|
|
|
var clock = nower(defaultClock{})
|
|
|
|
type cache struct {
|
|
patterns []Pattern
|
|
entries map[string]cacheEntry
|
|
}
|
|
|
|
type cacheEntry struct {
|
|
result Result
|
|
access time.Time
|
|
}
|
|
|
|
func newCache(patterns []Pattern) *cache {
|
|
return &cache{
|
|
patterns: patterns,
|
|
entries: make(map[string]cacheEntry),
|
|
}
|
|
}
|
|
|
|
func (c *cache) clean(d time.Duration) {
|
|
for k, v := range c.entries {
|
|
if clock.Now().Sub(v.access) > d {
|
|
delete(c.entries, k)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *cache) get(key string) (Result, bool) {
|
|
entry, ok := c.entries[key]
|
|
if ok {
|
|
entry.access = clock.Now()
|
|
c.entries[key] = entry
|
|
}
|
|
return entry.result, ok
|
|
}
|
|
|
|
func (c *cache) set(key string, result Result) {
|
|
c.entries[key] = cacheEntry{result, time.Now()}
|
|
}
|
|
|
|
func (c *cache) len() int {
|
|
l := len(c.entries)
|
|
return l
|
|
}
|
|
|
|
type defaultClock struct{}
|
|
|
|
func (defaultClock) Now() time.Time {
|
|
return time.Now()
|
|
}
|