Skip to content

Commit

Permalink
improve documentation and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pzentenoe committed Jul 3, 2024
1 parent 17d6c6f commit d517fd7
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 467 deletions.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.PHONY: install-tools check test

# Go tool paths
GOLINT = $(shell go env GOPATH)/bin/golint
INEFFASSIGN = $(shell go env GOPATH)/bin/ineffassign
MISSPELL = $(shell go env GOPATH)/bin/misspell
GOCYCLO = $(shell go env GOPATH)/bin/gocyclo

install-tools:
@echo "Installing tools..."
go install golang.org/x/lint/golint@latest
go install github.com/gordonklaus/ineffassign@latest
go install github.com/client9/misspell/cmd/misspell@latest
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest

check: install-tools
@echo "Running checks..."
go fmt ./...
go vet ./...
$(GOLINT) ./...
$(MISSPELL) -w .
$(GOCYCLO) -over 10 .
$(INEFFASSIGN) .

test:
go test ./...
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,5 @@ This project is released under the MIT License. See the [LICENSE](LICENSE) file
## Changelog
For a detailed changelog, refer to [CHANGELOG.md](CHANGELOG.md).

## Autor
## Author
- **Pablo Zenteno** - _Full Stack Developer_ - [pzentenoe](https://github.com/pzentenoe)
17 changes: 9 additions & 8 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
)

const (
// For use with functions that take an expiration time.
// NoExpiration For use with functions that take an expiration time.
NoExpiration time.Duration = -1
// For use with functions that take an expiration time. Equivalent to
// DefaultExpiration For use with functions that take an expiration time. Equivalent to
// passing in the same expiration duration as was given to New() or
// NewFrom() when the cache was created (e.g. 5 minutes.)
DefaultExpiration time.Duration = 0
)

// Cache struct for cache control
type Cache struct {
defaultExpiration time.Duration
items map[string]Item
Expand All @@ -23,7 +24,7 @@ type Cache struct {
janitor *janitor
}

// Add an item to the cache, replacing any existing item. If the duration is 0
// Set Add an item to the cache, replacing any existing item. If the duration is 0
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
// (NoExpiration), the item never expires.
func (c *Cache) Set(k string, x any, d time.Duration) {
Expand Down Expand Up @@ -56,7 +57,7 @@ func (c *Cache) set(k string, x any, d time.Duration) {
}
}

// Add an item to the cache, replacing any existing item, using the default
// SetDefault Add an item to the cache, replacing any existing item, using the default
// expiration.
func (c *Cache) SetDefault(k string, x any) {
c.Set(k, x, DefaultExpiration)
Expand All @@ -75,7 +76,7 @@ func (c *Cache) Add(k string, x any, d time.Duration) error {
return nil
}

// Set a new value for the cache key only if it already exists, and the existing
// Replace Set a new value for the cache key only if it already exists, and the existing
// item hasn't expired. Returns an error otherwise.
func (c *Cache) Replace(k string, x any, d time.Duration) error {
c.mu.Lock()
Expand Down Expand Up @@ -158,7 +159,7 @@ type keyAndValue struct {
value any
}

// Delete all expired items from the cache.
// DeleteExpired Delete all expired items from the cache.
func (c *Cache) DeleteExpired() {
var evictedItems []keyAndValue
c.mu.Lock()
Expand All @@ -176,7 +177,7 @@ func (c *Cache) DeleteExpired() {
}
}

// Sets an (optional) function that is called with the key and value when an
// OnEvicted Sets an (optional) function that is called with the key and value when an
// item is evicted from the cache. (Including when it is deleted manually, but
// not when it is overwritten.) Set to nil to disable.
func (c *Cache) OnEvicted(f func(string, any)) {
Expand All @@ -185,7 +186,7 @@ func (c *Cache) OnEvicted(f func(string, any)) {
c.onEvicted = f
}

// Delete all items from the cache.
// Flush Delete all items from the cache.
func (c *Cache) Flush() {
c.mu.Lock()
defer c.mu.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions cache_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func newCacheWithJanitor(de, ci time.Duration, m map[string]Item) *Cache {
return c
}

// Return a new cache with a given default expiration duration and cleanup
// New Return a new cache with a given default expiration duration and cleanup
// interval. If the expiration duration is less than one (or NoExpiration),
// the items in the cache never expire (by default), and must be deleted
// manually. If the cleanup interval is less than one, expired items are not
Expand All @@ -32,7 +32,7 @@ func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}

// Return a new cache with a given default expiration duration and cleanup
// NewFrom Return a new cache with a given default expiration duration and cleanup
// interval. If the expiration duration is less than one (or NoExpiration),
// the items in the cache never expire (by default), and must be deleted
// manually. If the cleanup interval is less than one, expired items are not
Expand Down
Loading

0 comments on commit d517fd7

Please sign in to comment.