-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
167 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,60 @@ | ||
// Copyright (c) 2023 - 2024 vistart. All rights reserved. | ||
// Use of this source code is governed by Apache-2.0 license | ||
// that can be found in the LICENSE file. | ||
|
||
// Package cache defines the cache interface and the simplest cache implementation. | ||
package cache | ||
|
||
// KeyGetter defines a method to retrieve keys. | ||
type KeyGetter interface { | ||
GetKey() string | ||
} | ||
|
||
// Interface defines a generic caching interface for getting, setting, deleting, and clearing caches. | ||
type Interface interface { | ||
// Get retrieves the value of the specified key from the cache. | ||
// | ||
// Parameter | ||
// | ||
// - key: an object implementing the KeyGetter interface to retrieve the key to get. | ||
// | ||
// Returns | ||
// | ||
// - any: the retrieved value, returned as an empty interface. | ||
// | ||
// - error: if an error occurs, it returns the corresponding error message. | ||
Get(key KeyGetter) (any, error) | ||
|
||
// Set sets the value of the specified key in the cache. | ||
// | ||
// Parameter | ||
// | ||
// - key: an object implementing the KeyGetter interface to retrieve the key to set. | ||
// | ||
// - value: the value to set, passed as an empty interface. | ||
// | ||
// - options: optional options to set cache items. | ||
// | ||
// Returns | ||
// | ||
// - error: if an error occurs, it returns the corresponding error message. | ||
Set(key KeyGetter, value any, options ...ItemOption) error | ||
|
||
// Delete deletes the value of the specified key from the cache. | ||
// | ||
// Parameter | ||
// | ||
// - key: an object implementing the KeyGetter interface to retrieve the key to delete. | ||
// | ||
// Returns | ||
// | ||
// - error: if an error occurs, it returns the corresponding error message. | ||
Delete(key KeyGetter) error | ||
|
||
// Clear clears the cache, deleting all cache items. | ||
// | ||
// Returns | ||
// | ||
// - error: if an error occurs, it returns the corresponding error message. | ||
Clear() error | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,29 +1,49 @@ | ||
// Copyright (c) 2023 - 2024 vistart. All rights reserved. | ||
// Use of this source code is governed by Apache-2.0 license | ||
// that can be found in the LICENSE file. | ||
|
||
package cache | ||
|
||
import "time" | ||
|
||
// ItemInterface defines an interface for cache items. | ||
type ItemInterface interface { | ||
// Expired checks if the item has expired. | ||
// | ||
// Returns | ||
// | ||
// - bool: true if the item has expired, otherwise false. | ||
Expired() bool | ||
|
||
// Value retrieves the value of the item. | ||
// | ||
// Returns | ||
// | ||
// - any: the value of the item. | ||
Value() any | ||
} | ||
|
||
// Item represents a cache item. | ||
type Item struct { | ||
value any | ||
expiredAt *time.Time | ||
ItemInterface | ||
} | ||
|
||
// Expired checks if the item has expired. | ||
func (i *Item) Expired() bool { | ||
if i.expiredAt == nil { | ||
return false | ||
} | ||
return i.expiredAt.Before(time.Now()) | ||
} | ||
|
||
// Value retrieves the value of the item. | ||
func (i *Item) Value() any { | ||
return i.value | ||
} | ||
|
||
// NewItem creates and initializes a new cache item with the given value. | ||
func NewItem(value any) *Item { | ||
return &Item{value: value} | ||
} |
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,53 @@ | ||
// Copyright (c) 2023 - 2024 vistart. All rights reserved. | ||
// Use of this source code is governed by Apache-2.0 license | ||
// that can be found in the LICENSE file. | ||
|
||
package cache | ||
|
||
import "time" | ||
|
||
// ErrInvalidTTL represents an error indicating an invalid Time To Live (TTL) value. | ||
type ErrInvalidTTL struct { | ||
error | ||
} | ||
|
||
// Error returns the error message for ErrInvalidTTL. | ||
func (e ErrInvalidTTL) Error() string { | ||
return "invalid TTL in cache" | ||
} | ||
|
||
// ErrInvalidExpiration represents an error indicating an invalid expiration time. | ||
type ErrInvalidExpiration struct { | ||
error | ||
} | ||
|
||
// Error returns the error message for ErrInvalidExpiration. | ||
func (e ErrInvalidExpiration) Error() string { | ||
return "invalid expiration in cache" | ||
} | ||
|
||
// ItemOption represents an option function that can be applied to cache items. | ||
type ItemOption func(*Item) error | ||
|
||
// WithTTL creates an ItemOption with a specified duration as the Time To Live (TTL) for the cache item. | ||
func WithTTL(duration time.Duration) ItemOption { | ||
return func(i *Item) error { | ||
if duration <= 0 { | ||
return ErrInvalidTTL{} | ||
} | ||
expiredAt := time.Now().Add(duration) | ||
i.expiredAt = &expiredAt | ||
return nil | ||
} | ||
} | ||
|
||
// WithExpiredAt creates an ItemOption with a specified expiration time for the cache item. | ||
func WithExpiredAt(expiredAt time.Time) ItemOption { | ||
return func(i *Item) error { | ||
if expiredAt.Before(time.Now()) { | ||
return ErrInvalidExpiration{} | ||
} | ||
i.expiredAt = &expiredAt | ||
return nil | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.