Skip to content

Commit

Permalink
[cache/memory] Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jul 30, 2024
1 parent 7006b4d commit ce21b06
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cache/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *Cache) Expired() int {

// Set adds or updates item in cache
func (c *Cache) Set(key string, data any, expiration ...time.Duration) bool {
if c == nil {
if c == nil || data == nil {
return false
}

Expand Down Expand Up @@ -279,11 +279,11 @@ func (c *Cache) Flush() bool {
// Validate validates cache configuration
func (c Config) Validate() error {
if c.DefaultExpiration < MIN_EXPIRATION {
return errors.New("Expiration is too short (< 1ms)")
return errors.New("Invalid configuration: Expiration is too short (< 1ms)")
}

if c.CleanupInterval != 0 && c.CleanupInterval < MIN_CLEANUP_INTERVAL {
return errors.New("Cleanup interval is too short (< 1ms)")
return errors.New("Invalid configuration: Cleanup interval is too short (< 1ms)")
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions cache/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *CacheSuite) TestCache(c *C) {
c.Assert(cache.Get("2"), Equals, nil)
c.Assert(item, Equals, nil)

cache.Flush()
c.Assert(cache.Flush(), Equals, true)
}

func (s *CacheSuite) TestCacheWithoutJanitor(c *C) {
Expand Down Expand Up @@ -135,9 +135,9 @@ func (s *CacheSuite) TestNil(c *C) {
func (s *CacheSuite) TestConfig(c *C) {
_, err := New(Config{DefaultExpiration: 1})

c.Assert(err.Error(), Equals, "Expiration is too short (< 1ms)")
c.Assert(err.Error(), Equals, "Invalid configuration: Expiration is too short (< 1ms)")

_, err = New(Config{DefaultExpiration: time.Minute, CleanupInterval: 1})

c.Assert(err.Error(), Equals, "Cleanup interval is too short (< 1ms)")
c.Assert(err.Error(), Equals, "Invalid configuration: Cleanup interval is too short (< 1ms)")
}

0 comments on commit ce21b06

Please sign in to comment.