Skip to content

Commit

Permalink
Merge pull request #492 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 13.3.1
  • Loading branch information
andyone authored Jul 31, 2024
2 parents 2b5b082 + d736b2b commit 4f53528
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 135 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## 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`
- `[cache/fs]` Added check for passed expiration duration in `Set`
- `[cache/fs]` Code refactoring
- `[cache/fs]` Fixed bug with closing item file after reading data
- `[cache/fs]` Fixed bug with removing expired items in `Get`

### [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"))
}
59 changes: 36 additions & 23 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 @@ -87,15 +87,6 @@ func New(config Config) (*Cache, error) {

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

// Has returns true if cache contains data for given key
func (c *Cache) Has(key string) bool {
if c == nil || key == "" {
return false
}

return fsutil.IsExist(c.getItemPath(key, false))
}

// Size returns number of items in cache
func (c *Cache) Size() int {
if c == nil {
Expand Down Expand Up @@ -128,8 +119,17 @@ func (c *Cache) Expired() int {
return expired
}

// Has returns true if cache contains data for given key
func (c *Cache) Has(key string) bool {
if c == nil || key == "" {
return false
}

return fsutil.IsExist(c.getItemPath(key, false))
}

// 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 All @@ -152,16 +152,14 @@ func (c *Cache) Set(key string, data any, expiration ...time.Duration) bool {

itemFile := c.getItemPath(key, false)

err = os.Rename(tmpFile, itemFile)

if err != nil {
if os.Rename(tmpFile, itemFile) != nil {
os.Remove(tmpFile)
return false
}

expr := c.expiration

if len(expiration) > 0 {
if len(expiration) > 0 && expiration[0] >= MIN_EXPIRATION {
expr = expiration[0]
}

Expand All @@ -170,7 +168,14 @@ func (c *Cache) Set(key string, data any, expiration ...time.Duration) bool {

// GetWithExpiration returns item from cache
func (c *Cache) Get(key string) any {
if c == nil || key == "" {
if c == nil || key == "" || !c.Has(key) {
return nil
}

expr := c.GetExpiration(key)

if !expr.IsZero() && expr.Before(time.Now()) {
c.Delete(key)
return nil
}

Expand All @@ -183,6 +188,8 @@ func (c *Cache) Get(key string) any {
item := &cacheItem{}
err = gob.NewDecoder(fd).Decode(item)

fd.Close()

if err != nil {
return nil
}
Expand All @@ -192,7 +199,7 @@ func (c *Cache) Get(key string) any {

// GetWithExpiration returns item expiration date
func (c *Cache) GetExpiration(key string) time.Time {
if c == nil || key == "" {
if c == nil || key == "" || !c.Has(key) {
return time.Time{}
}

Expand All @@ -203,11 +210,17 @@ func (c *Cache) GetExpiration(key string) time.Time {

// GetWithExpiration returns item from cache and expiration date or nil
func (c *Cache) GetWithExpiration(key string) (any, time.Time) {
if c == nil || key == "" {
if c == nil || key == "" || !c.Has(key) {
return nil, time.Time{}
}

return c.Get(key), c.GetExpiration(key)
data := c.Get(key)

if data != nil {
return data, c.GetExpiration(key)
}

return nil, time.Time{}
}

// Delete removes item from cache
Expand Down
12 changes: 7 additions & 5 deletions cache/fs/fs_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ package fs

import (
"time"

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

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

// ❗ 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

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

Expand All @@ -31,8 +33,8 @@ type Cache struct{}
// ❗ 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 @@ -60,7 +62,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 {
panic("UNSUPPORTED")
}

Expand Down
Loading

0 comments on commit 4f53528

Please sign in to comment.