Skip to content

Commit

Permalink
add v2 directory as preparation for generics implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Aug 4, 2022
1 parent eaa2cb3 commit 9ca36f2
Show file tree
Hide file tree
Showing 25 changed files with 3,569 additions and 1 deletion.
56 changes: 56 additions & 0 deletions .github/workflows/ci-v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: build-v2

on:
push:
branches:
tags:
paths:
- ".github/workflows/ci-v2.yml"
- "v2/**"
pull_request:
paths:
- ".github/workflows/ci-v2.yml"
- "v2/**"

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: set up go 1.18
uses: actions/setup-go@v3
with:
go-version: 1.18
id: go

- name: checkout
uses: actions/checkout@v2

- name: start Redis
uses: supercharge/redis-github-action@1.4.0

- name: build and test
run: |
go get -v
go test -timeout=60s -race -covermode=atomic -coverprofile=$GITHUB_WORKSPACE/profile.cov_tmp
cat $GITHUB_WORKSPACE/profile.cov_tmp | grep -v "_mock.go" > $GITHUB_WORKSPACE/profile.cov
go build -race
working-directory: v2
env:
TZ: "America/Chicago"
ENABLE_REDIS_TESTS: "true"

- name: install golangci-lint and goveralls
run: |
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GITHUB_WORKSPACE v1.47.2
go install github.com/mattn/goveralls@latest
- name: run linters
run: $GITHUB_WORKSPACE/golangci-lint run --out-format=github-actions
working-directory: v2

- name: submit coverage
run: $(go env GOPATH)/bin/goveralls -service="github" -coverprofile=$GITHUB_WORKSPACE/profile.cov
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
working-directory: v2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Main features:

## Install and update

`go get -u github.com/go-pkgz/lcw`
`go get -u github.com/go-pkgz/lcw/v2`

## Usage

Expand Down
55 changes: 55 additions & 0 deletions v2/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
linters-settings:
govet:
check-shadowing: true
gocyclo:
min-complexity: 15
maligned:
suggest-new: true
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc

linters:
enable:
- megacheck
- revive
- govet
- unconvert
- megacheck
- structcheck
- gas
- gocyclo
- dupl
- misspell
- unparam
- varcheck
- deadcode
- typecheck
- ineffassign
- varcheck
- stylecheck
- gochecknoinits
- exportloopref
- gocritic
- nakedret
- gosimple
- prealloc
fast: false
disable-all: true

run:
output:
format: tab
skip-dirs:
- vendor
86 changes: 86 additions & 0 deletions v2/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Package lcw adds a thin layer on top of lru and expirable cache providing more limits and common interface.
// The primary method to get (and set) data to/from the cache is LoadingCache.Get returning stored data for a given key or
// call provided func to retrieve and store, similar to Guava loading cache.
// Limits allow max values for key size, number of keys, value size and total size of values in the cache.
// CacheStat gives general stats on cache performance.
// 3 flavors of cache provided - NoP (do-nothing cache), ExpirableCache (TTL based), and LruCache
package lcw

//go:generate sh -c "mockery -inpkg -name LoadingCache -print > /tmp/cache-mock.tmp && mv /tmp/cache-mock.tmp cache_mock.go"

import (
"fmt"
)

// Sizer allows to perform size-based restrictions, optional.
// If not defined both maxValueSize and maxCacheSize checks will be ignored
type Sizer interface {
Size() int
}

// LoadingCache defines guava-like cache with Get method returning cached value ao retrieving it if not in cache
type LoadingCache interface {
Get(key string, fn func() (interface{}, error)) (val interface{}, err error) // load or get from cache
Peek(key string) (interface{}, bool) // get from cache by key
Invalidate(fn func(key string) bool) // invalidate items for func(key) == true
Delete(key string) // delete by key
Purge() // clear cache
Stat() CacheStat // cache stats
Keys() []string // list of all keys
Close() error // close open connections
}

// CacheStat represent stats values
type CacheStat struct {
Hits int64
Misses int64
Keys int
Size int64
Errors int64
}

// String formats cache stats
func (s CacheStat) String() string {
ratio := 0.0
if s.Hits+s.Misses > 0 {
ratio = float64(s.Hits) / float64(s.Hits+s.Misses)
}
return fmt.Sprintf("{hits:%d, misses:%d, ratio:%.2f, keys:%d, size:%d, errors:%d}",
s.Hits, s.Misses, ratio, s.Keys, s.Size, s.Errors)
}

// Nop is do-nothing implementation of LoadingCache
type Nop struct{}

// NewNopCache makes new do-nothing cache
func NewNopCache() *Nop {
return &Nop{}
}

// Get calls fn without any caching
func (n *Nop) Get(key string, fn func() (interface{}, error)) (interface{}, error) { return fn() }

// Peek does nothing and always returns false
func (n *Nop) Peek(key string) (interface{}, bool) { return nil, false }

// Invalidate does nothing for nop cache
func (n *Nop) Invalidate(fn func(key string) bool) {}

// Purge does nothing for nop cache
func (n *Nop) Purge() {}

// Delete does nothing for nop cache
func (n *Nop) Delete(key string) {}

// Keys does nothing for nop cache
func (n *Nop) Keys() []string { return nil }

// Stat always 0s for nop cache
func (n *Nop) Stat() CacheStat {
return CacheStat{}
}

// Close does nothing for nop cache
func (n *Nop) Close() error {
return nil
}
Loading

0 comments on commit 9ca36f2

Please sign in to comment.