Skip to content

Commit

Permalink
[cache] Add cache.Duration as alias to time.Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jul 31, 2024
1 parent c26c58e commit 1825015
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 109 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### [13.3.1](https://kaos.sh/ek/13.3.1)

- `[cache]` Added constants with duration
- `[cache/fs]` Using `cache.Duration` instead of `time.Duration`
- `[cache/memory]` Using `cache.Duration` instead of `time.Duration`

### [13.3.0](https://kaos.sh/ek/13.3.0)

- `[cache/fs]` Added cache with file system storage
Expand Down
18 changes: 18 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import "time"

// ////////////////////////////////////////////////////////////////////////////////// //

// Duration is time.Duration alias
type Duration = time.Duration

// ////////////////////////////////////////////////////////////////////////////////// //

// Cache is cache backend interface
type Cache interface {
// Has returns true if cache contains data for given key
Expand Down Expand Up @@ -41,3 +46,16 @@ type Cache interface {
// Flush removes all data from cache
Flush() bool
}

// ////////////////////////////////////////////////////////////////////////////////// //

const (
MILLISECOND = time.Millisecond // 1 ms
SECOND = time.Second // 1 sec
MINUTE = time.Minute // 1 min
HOUR = time.Hour // 1 hr
DAY = 24 * HOUR // 24 hr
WEEK = 7 * DAY // 7 d
MONTH = 30 * DAY // 30 d
YEAR = 365 * DAY // 365 d
)
99 changes: 50 additions & 49 deletions cache/fs/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,120 +9,121 @@ package fs

import (
"fmt"
"time"

"github.com/essentialkaos/ek/v13/cache"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleNew() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
c.Set("test", "ABCD")

fmt.Println(cache.Get("test"))
fmt.Println(c.Get("test"))
}

func ExampleCache_Set() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
cache.Set("test", "ABCD", 15*time.Minute)
c.Set("test", "ABCD")
c.Set("test", "ABCD", 15*cache.MINUTE)

fmt.Println(cache.Get("test"))
fmt.Println(c.Get("test"))
}

func ExampleCache_Has() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
c.Set("test", "ABCD")

fmt.Println(cache.Has("test"))
fmt.Println(c.Has("test"))
}

func ExampleCache_Get() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
c.Set("test", "ABCD")

fmt.Println(cache.Get("test"))
fmt.Println(c.Get("test"))
}

func ExampleCache_Size() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
c.Set("test", "ABCD")

fmt.Println(cache.Size())
fmt.Println(c.Size())
}

func ExampleCache_Expired() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
c.Set("test", "ABCD")

fmt.Println(cache.Expired())
fmt.Println(c.Expired())
}

func ExampleCache_GetWithExpiration() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
c.Set("test", "ABCD")

item, exp := cache.GetWithExpiration("test")
item, exp := c.GetWithExpiration("test")

fmt.Println(item, exp.String())
}

func ExampleCache_Delete() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
cache.Delete("test")
c.Set("test", "ABCD")
c.Delete("test")

fmt.Println(cache.Get("test"))
fmt.Println(c.Get("test"))
}

func ExampleCache_Flush() {
cache, _ := New(Config{
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
})

cache.Set("test", "ABCD")
cache.Flush()
c.Set("test", "ABCD")
c.Flush()

fmt.Println(cache.Get("test"))
fmt.Println(c.Get("test"))
}
12 changes: 6 additions & 6 deletions cache/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// MIN_EXPIRATION is minimal expiration duration
const MIN_EXPIRATION = time.Second
const MIN_EXPIRATION = cache.SECOND

// MIN_CLEANUP_INTERVAL is minimal cleanup interval
const MIN_CLEANUP_INTERVAL = time.Second
const MIN_CLEANUP_INTERVAL = cache.SECOND

// ////////////////////////////////////////////////////////////////////////////////// //

// Cache is fs cache instance
type Cache struct {
dir string
hasher hash.Hash
expiration time.Duration
expiration cache.Duration
isJanitorWorks bool
}

// Config is cache configuration
type Config struct {
Dir string
DefaultExpiration time.Duration
CleanupInterval time.Duration
DefaultExpiration cache.Duration
CleanupInterval cache.Duration
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -129,7 +129,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 {
func (c *Cache) Set(key string, data any, expiration ...cache.Duration) bool {
if c == nil || data == nil || key == "" {
return false
}
Expand Down
Loading

0 comments on commit 1825015

Please sign in to comment.