2014-12-02 15:13:03 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// 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,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-12-02 15:13:03 -07:00
|
|
|
|
|
|
|
package ignore
|
|
|
|
|
2014-12-23 02:05:08 -07:00
|
|
|
import "time"
|
2014-12-02 15:13:03 -07:00
|
|
|
|
2017-06-11 03:27:12 -07:00
|
|
|
type nower interface {
|
|
|
|
Now() time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var clock = nower(defaultClock{})
|
|
|
|
|
2014-12-02 15:13:03 -07:00
|
|
|
type cache struct {
|
|
|
|
patterns []Pattern
|
|
|
|
entries map[string]cacheEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type cacheEntry struct {
|
2016-04-07 02:34:07 -07:00
|
|
|
result Result
|
2014-12-02 15:13:03 -07:00
|
|
|
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 {
|
2017-06-11 03:27:12 -07:00
|
|
|
if clock.Now().Sub(v.access) > d {
|
2014-12-02 15:13:03 -07:00
|
|
|
delete(c.entries, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 02:34:07 -07:00
|
|
|
func (c *cache) get(key string) (Result, bool) {
|
|
|
|
entry, ok := c.entries[key]
|
2014-12-02 15:13:03 -07:00
|
|
|
if ok {
|
2017-06-11 03:27:12 -07:00
|
|
|
entry.access = clock.Now()
|
2016-04-07 02:34:07 -07:00
|
|
|
c.entries[key] = entry
|
2014-12-02 15:13:03 -07:00
|
|
|
}
|
2016-04-07 02:34:07 -07:00
|
|
|
return entry.result, ok
|
2014-12-02 15:13:03 -07:00
|
|
|
}
|
|
|
|
|
2016-04-07 02:34:07 -07:00
|
|
|
func (c *cache) set(key string, result Result) {
|
|
|
|
c.entries[key] = cacheEntry{result, time.Now()}
|
2014-12-02 15:13:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) len() int {
|
|
|
|
l := len(c.entries)
|
|
|
|
return l
|
|
|
|
}
|
2017-06-11 03:27:12 -07:00
|
|
|
|
|
|
|
type defaultClock struct{}
|
|
|
|
|
|
|
|
func (defaultClock) Now() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|