-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* clear after stop * fixing data races * adding atomic cache * fixing lint errors * fixing imports
- Loading branch information
Showing
7 changed files
with
111 additions
and
23 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
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
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
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
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
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
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,62 @@ | ||
package atomcache | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/karlseguin/ccache" | ||
) | ||
|
||
type Cache struct { | ||
*ccache.Cache | ||
Closed atomic.Bool | ||
mu sync.RWMutex | ||
} | ||
|
||
func NewWithCache(c *ccache.Cache) *Cache { | ||
return &Cache{Cache: c} | ||
} | ||
|
||
func (c *Cache) Get(key string) *ccache.Item { | ||
if c.Closed.Load() { | ||
return nil | ||
} | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
|
||
return c.Cache.Get(key) | ||
} | ||
|
||
func (c *Cache) Set(key string, value interface{}, duration time.Duration) { | ||
if c.Closed.Load() { | ||
return | ||
} | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.Cache.Set(key, value, duration) | ||
} | ||
|
||
func (c *Cache) Delete(key string) bool { | ||
if c.Closed.Load() { | ||
return false | ||
} | ||
|
||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
return c.Cache.Delete(key) | ||
} | ||
|
||
func (c *Cache) Stop() { | ||
if c.Closed.Load() { | ||
return | ||
} | ||
c.Closed.Store(true) | ||
|
||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.Cache.Stop() | ||
} |