forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize code by write a cache package
Signed-off-by: Jianhui Zhao <[email protected]>
- Loading branch information
Jianhui Zhao
committed
Apr 26, 2019
1 parent
700ea91
commit 80cee19
Showing
2 changed files
with
90 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cache | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Item struct { | ||
value interface{} | ||
expiration int64 | ||
} | ||
|
||
type Cache struct { | ||
items sync.Map | ||
defaultExpiration time.Duration | ||
gcInterval time.Duration | ||
stop chan struct{} | ||
} | ||
|
||
// Delete all expired items from the cache. | ||
func (c *Cache) DeleteExpired() { | ||
now := time.Now().UnixNano() | ||
|
||
c.items.Range(func(key, value interface{}) bool { | ||
if value := value.(*Item); value.expiration > 0 && now > value.expiration { | ||
c.items.Delete(key) | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
func (c *Cache) gcLoop() { | ||
ticker := time.NewTicker(c.gcInterval) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
c.DeleteExpired() | ||
case <-c.stop: | ||
ticker.Stop() | ||
return | ||
} | ||
} | ||
} | ||
|
||
func New(defaultExpiration, gcInterval time.Duration) *Cache { | ||
c := &Cache{ | ||
defaultExpiration: defaultExpiration, | ||
gcInterval: gcInterval, | ||
stop: make(chan struct{}), | ||
} | ||
|
||
go c.gcLoop() | ||
|
||
runtime.SetFinalizer(c, func(c *Cache) { | ||
c.stop <- struct{}{} | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Cache) Set(key, value interface{}, d time.Duration) { | ||
var e int64 | ||
|
||
if d == 0 { | ||
d = c.defaultExpiration | ||
} | ||
|
||
if d > 0 { | ||
e = time.Now().Add(d).UnixNano() | ||
} | ||
|
||
c.items.Store(key, &Item{value, e}) | ||
} | ||
|
||
func (c *Cache) Get(key interface{}) (interface{}, bool) { | ||
return c.items.Load(key) | ||
} | ||
|
||
func (c *Cache) Del(key interface{}) { | ||
c.items.Delete(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters