Merge 5b9bc4754a
into 46f4078530
This commit is contained in:
commit
bee988153b
19
cache.go
19
cache.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -15,7 +16,7 @@ type Item struct {
|
||||||
Expiration int64
|
Expiration int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the item has expired.
|
// Expired Returns true if the item has expired.
|
||||||
func (item Item) Expired() bool {
|
func (item Item) Expired() bool {
|
||||||
if item.Expiration == 0 {
|
if item.Expiration == 0 {
|
||||||
return false
|
return false
|
||||||
|
@ -24,9 +25,9 @@ func (item Item) Expired() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// For use with functions that take an expiration time.
|
// NoExpiration For use with functions that take an expiration time.
|
||||||
NoExpiration time.Duration = -1
|
NoExpiration time.Duration = -1
|
||||||
// For use with functions that take an expiration time. Equivalent to
|
// DefaultExpiration For use with functions that take an expiration time. Equivalent to
|
||||||
// passing in the same expiration duration as was given to New() or
|
// passing in the same expiration duration as was given to New() or
|
||||||
// NewFrom() when the cache was created (e.g. 5 minutes.)
|
// NewFrom() when the cache was created (e.g. 5 minutes.)
|
||||||
DefaultExpiration time.Duration = 0
|
DefaultExpiration time.Duration = 0
|
||||||
|
@ -45,7 +46,7 @@ type cache struct {
|
||||||
janitor *janitor
|
janitor *janitor
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add an item to the cache, replacing any existing item. If the duration is 0
|
// Set Add an item to the cache, replacing any existing item. If the duration is 0
|
||||||
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
|
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
|
||||||
// (NoExpiration), the item never expires.
|
// (NoExpiration), the item never expires.
|
||||||
func (c *cache) Set(k string, x interface{}, d time.Duration) {
|
func (c *cache) Set(k string, x interface{}, d time.Duration) {
|
||||||
|
@ -911,6 +912,16 @@ func (c *cache) Delete(k string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete an item from the cache by regex rule
|
||||||
|
func (c *cache) DeleteRegex(rule string) {
|
||||||
|
re, _ := regexp.Compile(rule)
|
||||||
|
for k := range c.items {
|
||||||
|
if re.MatchString(k) {
|
||||||
|
c.Delete(k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *cache) delete(k string) (interface{}, bool) {
|
func (c *cache) delete(k string) (interface{}, bool) {
|
||||||
if c.onEvicted != nil {
|
if c.onEvicted != nil {
|
||||||
if v, found := c.items[k]; found {
|
if v, found := c.items[k]; found {
|
||||||
|
|
|
@ -1150,6 +1150,19 @@ func TestDelete(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeleteRegex(t *testing.T) {
|
||||||
|
tc := New(DefaultExpiration, 0)
|
||||||
|
tc.Set("foo1", "bar", DefaultExpiration)
|
||||||
|
tc.DeleteRegex(`^foo`)
|
||||||
|
x, found := tc.Get("foo")
|
||||||
|
if found {
|
||||||
|
t.Error("foo was found, but it should have been deleted")
|
||||||
|
}
|
||||||
|
if x != nil {
|
||||||
|
t.Error("x is not nil:", x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestItemCount(t *testing.T) {
|
func TestItemCount(t *testing.T) {
|
||||||
tc := New(DefaultExpiration, 0)
|
tc := New(DefaultExpiration, 0)
|
||||||
tc.Set("foo", "1", DefaultExpiration)
|
tc.Set("foo", "1", DefaultExpiration)
|
||||||
|
|
Loading…
Reference in New Issue