Skip to content

Commit

Permalink
Code quality improvements (#67)
Browse files Browse the repository at this point in the history
* add golangci-lint configuration, fix discovered issues

* add GitHub Actions test and build and linter run
  • Loading branch information
paskal committed May 11, 2020
1 parent 14eae34 commit eb52994
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 14 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,32 @@
name: build

on:
push:
branches:
tags:
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: set up go 1.14
uses: actions/setup-go@v1
with:
go-version: 1.14
id: go

- name: checkout
uses: actions/checkout@v2

- name: build and test
run: |
go test -timeout=60s -race
go build -race
- name: install golangci-lint
run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GITHUB_WORKSPACE v1.26.0

- name: run golangci-lint
run: $GITHUB_WORKSPACE/golangci-lint run --out-format=github-actions
33 changes: 33 additions & 0 deletions .golangci.yml
@@ -0,0 +1,33 @@
linters:
enable:
- megacheck
- golint
- govet
- unconvert
- megacheck
- structcheck
- gas
- gocyclo
- dupl
- misspell
- unparam
- varcheck
- deadcode
- typecheck
- ineffassign
- varcheck
- stylecheck
- scopelint
- gocritic
- nakedret
- gosimple
- prealloc
fast: false
disable-all: true

issues:
exclude-rules:
- path: _test\.go
linters:
- dupl
exclude-use-default: false
3 changes: 1 addition & 2 deletions 2q.go
Expand Up @@ -44,7 +44,7 @@ func New2Q(size int) (*TwoQueueCache, error) {

// New2QParams creates a new TwoQueueCache using the provided
// parameter values.
func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCache, error) {
func New2QParams(size int, recentRatio, ghostRatio float64) (*TwoQueueCache, error) {
if size <= 0 {
return nil, fmt.Errorf("invalid size")
}
Expand Down Expand Up @@ -138,7 +138,6 @@ func (c *TwoQueueCache) Add(key, value interface{}) {
// Add to the recently seen list
c.ensureSpace(false)
c.recent.Add(key, value)
return
}

// ensureSpace is used to ensure we have space in the cache
Expand Down
1 change: 0 additions & 1 deletion arc.go
Expand Up @@ -173,7 +173,6 @@ func (c *ARCCache) Add(key, value interface{}) {

// Add to the recently seen list
c.t1.Add(key, value)
return
}

// replace is used to adaptively evict from either T1 or T2
Expand Down
4 changes: 2 additions & 2 deletions lru.go
Expand Up @@ -118,15 +118,15 @@ func (c *Cache) Resize(size int) (evicted int) {
}

// RemoveOldest removes the oldest item from the cache.
func (c *Cache) RemoveOldest() (key interface{}, value interface{}, ok bool) {
func (c *Cache) RemoveOldest() (key, value interface{}, ok bool) {
c.lock.Lock()
key, value, ok = c.lru.RemoveOldest()
c.lock.Unlock()
return
}

// GetOldest returns the oldest entry
func (c *Cache) GetOldest() (key interface{}, value interface{}, ok bool) {
func (c *Cache) GetOldest() (key, value interface{}, ok bool) {
c.lock.Lock()
key, value, ok = c.lru.GetOldest()
c.lock.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions lru_test.go
Expand Up @@ -267,7 +267,7 @@ func TestLRUResize(t *testing.T) {
// Downsize
l.Add(1, 1)
l.Add(2, 2)
evicted := l.Resize(1);
evicted := l.Resize(1)
if evicted != 1 {
t.Errorf("1 element should have been evicted: %v", evicted)
}
Expand All @@ -281,7 +281,7 @@ func TestLRUResize(t *testing.T) {
}

// Upsize
evicted = l.Resize(2);
evicted = l.Resize(2)
if evicted != 0 {
t.Errorf("0 elements should have been evicted: %v", evicted)
}
Expand Down
6 changes: 3 additions & 3 deletions simplelru/lru.go
Expand Up @@ -25,7 +25,7 @@ type entry struct {
// NewLRU constructs an LRU of the given size
func NewLRU(size int, onEvict EvictCallback) (*LRU, error) {
if size <= 0 {
return nil, errors.New("Must provide a positive size")
return nil, errors.New("must provide a positive size")
}
c := &LRU{
size: size,
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *LRU) Remove(key interface{}) (present bool) {
}

// RemoveOldest removes the oldest item from the cache.
func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool) {
func (c *LRU) RemoveOldest() (key, value interface{}, ok bool) {
ent := c.evictList.Back()
if ent != nil {
c.removeElement(ent)
Expand All @@ -120,7 +120,7 @@ func (c *LRU) RemoveOldest() (key interface{}, value interface{}, ok bool) {
}

// GetOldest returns the oldest entry
func (c *LRU) GetOldest() (key interface{}, value interface{}, ok bool) {
func (c *LRU) GetOldest() (key, value interface{}, ok bool) {
ent := c.evictList.Back()
if ent != nil {
kv := ent.Value.(*entry)
Expand Down
5 changes: 3 additions & 2 deletions simplelru/lru_interface.go
@@ -1,3 +1,4 @@
// Package simplelru provides simple LRU implementation based on build-in container/list.
package simplelru

// LRUCache is the interface for simple LRU cache.
Expand Down Expand Up @@ -34,6 +35,6 @@ type LRUCache interface {
// Clears all cache entries.
Purge()

// Resizes cache, returning number evicted
Resize(int) int
// Resizes cache, returning number evicted
Resize(int) int
}
4 changes: 2 additions & 2 deletions simplelru/lru_test.go
Expand Up @@ -180,7 +180,7 @@ func TestLRU_Resize(t *testing.T) {
// Downsize
l.Add(1, 1)
l.Add(2, 2)
evicted := l.Resize(1);
evicted := l.Resize(1)
if evicted != 1 {
t.Errorf("1 element should have been evicted: %v", evicted)
}
Expand All @@ -194,7 +194,7 @@ func TestLRU_Resize(t *testing.T) {
}

// Upsize
evicted = l.Resize(2);
evicted = l.Resize(2)
if evicted != 0 {
t.Errorf("0 elements should have been evicted: %v", evicted)
}
Expand Down

0 comments on commit eb52994

Please sign in to comment.