Initial commit
This commit is contained in:
21
vendor/github.com/akyoto/cache/.drone.yml
generated
vendored
Normal file
21
vendor/github.com/akyoto/cache/.drone.yml
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
kind: pipeline
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang
|
||||
commands:
|
||||
- go version
|
||||
- go get ./...
|
||||
- go build ./...
|
||||
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.6
|
||||
- golangci-lint run --enable dupl --enable goconst --enable gocritic --enable misspell --enable prealloc --enable unconvert --enable unparam
|
||||
- go test -v -race -coverprofile=coverage.txt ./...
|
||||
|
||||
- name: coverage
|
||||
image: plugins/codecov
|
||||
settings:
|
||||
token:
|
||||
from_secret: codecov-token
|
||||
files:
|
||||
- coverage.txt
|
||||
7
vendor/github.com/akyoto/cache/.gitignore
generated
vendored
Normal file
7
vendor/github.com/akyoto/cache/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
*.test
|
||||
*.out
|
||||
114
vendor/github.com/akyoto/cache/Cache.go
generated
vendored
Normal file
114
vendor/github.com/akyoto/cache/Cache.go
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cache stores arbitrary data with expiration time.
|
||||
type Cache struct {
|
||||
items sync.Map
|
||||
close chan struct{}
|
||||
}
|
||||
|
||||
// An item represents arbitrary data with expiration time.
|
||||
type item struct {
|
||||
data interface{}
|
||||
expires int64
|
||||
}
|
||||
|
||||
// New creates a new cache that asynchronously cleans
|
||||
// expired entries after the given time passes.
|
||||
func New(cleaningInterval time.Duration) *Cache {
|
||||
cache := &Cache{
|
||||
close: make(chan struct{}),
|
||||
}
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(cleaningInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
now := time.Now().UnixNano()
|
||||
|
||||
cache.items.Range(func(key, value interface{}) bool {
|
||||
item := value.(item)
|
||||
|
||||
if item.expires > 0 && now > item.expires {
|
||||
cache.items.Delete(key)
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
case <-cache.close:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return cache
|
||||
}
|
||||
|
||||
// Get gets the value for the given key.
|
||||
func (cache *Cache) Get(key interface{}) (interface{}, bool) {
|
||||
obj, exists := cache.items.Load(key)
|
||||
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
item := obj.(item)
|
||||
|
||||
if item.expires > 0 && time.Now().UnixNano() > item.expires {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return item.data, true
|
||||
}
|
||||
|
||||
// Set sets a value for the given key with an expiration duration.
|
||||
// If the duration is 0 or less, it will be stored forever.
|
||||
func (cache *Cache) Set(key interface{}, value interface{}, duration time.Duration) {
|
||||
var expires int64
|
||||
|
||||
if duration > 0 {
|
||||
expires = time.Now().Add(duration).UnixNano()
|
||||
}
|
||||
|
||||
cache.items.Store(key, item{
|
||||
data: value,
|
||||
expires: expires,
|
||||
})
|
||||
}
|
||||
|
||||
// Range calls f sequentially for each key and value present in the cache.
|
||||
// If f returns false, range stops the iteration.
|
||||
func (cache *Cache) Range(f func(key, value interface{}) bool) {
|
||||
now := time.Now().UnixNano()
|
||||
|
||||
fn := func(key, value interface{}) bool {
|
||||
item := value.(item)
|
||||
|
||||
if item.expires > 0 && now > item.expires {
|
||||
return true
|
||||
}
|
||||
|
||||
return f(key, item.data)
|
||||
}
|
||||
|
||||
cache.items.Range(fn)
|
||||
}
|
||||
|
||||
// Delete deletes the key and its value from the cache.
|
||||
func (cache *Cache) Delete(key interface{}) {
|
||||
cache.items.Delete(key)
|
||||
}
|
||||
|
||||
// Close closes the cache and frees up resources.
|
||||
func (cache *Cache) Close() {
|
||||
cache.close <- struct{}{}
|
||||
cache.items = sync.Map{}
|
||||
}
|
||||
18
vendor/github.com/akyoto/cache/LICENSE
generated
vendored
Normal file
18
vendor/github.com/akyoto/cache/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
Sponsorship License
|
||||
|
||||
Copyright (c) 2019 Eduard Urbach
|
||||
|
||||
* You are permitted to use the software freely for non-commercial purposes.
|
||||
* You MUST be a sponsor donating at least 10 USD every month to me if you use the software for commercial purposes.
|
||||
|
||||
To become a sponsor:
|
||||
|
||||
1. Visit the following page: https://github.com/users/akyoto/sponsorship
|
||||
2. Register as a sponsor for the 10 USD per month tier.
|
||||
|
||||
Additional notes:
|
||||
|
||||
* Canceling your sponsorship will also revoke the rights to use the software for commercial purposes.
|
||||
* The software is provided "as is", without warranty of any kind.
|
||||
* The above copyright notice and this permission must be included in all copies of the software.
|
||||
|
||||
62
vendor/github.com/akyoto/cache/README.md
generated
vendored
Normal file
62
vendor/github.com/akyoto/cache/README.md
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
# cache
|
||||
|
||||
[![Godoc][godoc-image]][godoc-url]
|
||||
[![Report][report-image]][report-url]
|
||||
[![Tests][tests-image]][tests-url]
|
||||
[![Coverage][coverage-image]][coverage-url]
|
||||
[![Sponsor][sponsor-image]][sponsor-url]
|
||||
|
||||
Cache arbitrary data with an expiration time.
|
||||
|
||||
## Features
|
||||
|
||||
* 0 dependencies
|
||||
* About 100 lines of code
|
||||
* 100% test coverage
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
// New cache
|
||||
c := cache.New(5 * time.Minute)
|
||||
|
||||
// Put something into the cache
|
||||
c.Set("a", "b", 1 * time.Minute)
|
||||
|
||||
// Read from the cache
|
||||
obj, found := c.Get("a")
|
||||
|
||||
// Convert the type
|
||||
fmt.Println(obj.(string))
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
||||
```text
|
||||
BenchmarkGet-12 300000000 3.88 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkSet-12 10000000 183 ns/op 48 B/op 2 allocs/op
|
||||
BenchmarkNew-12 10000000 112 ns/op 352 B/op 5 allocs/op
|
||||
```
|
||||
|
||||
## Style
|
||||
|
||||
Please take a look at the [style guidelines](https://github.com/akyoto/quality/blob/master/STYLE.md) if you'd like to make a pull request.
|
||||
|
||||
## Sponsors
|
||||
|
||||
| [](https://github.com/cedricfung) | [](https://github.com/soulcramer) | [](https://eduardurbach.com) |
|
||||
| --- | --- | --- |
|
||||
| [Cedric Fung](https://github.com/cedricfung) | [Scott Rayapoullé](https://github.com/soulcramer) | [Eduard Urbach](https://eduardurbach.com) |
|
||||
|
||||
Want to see [your own name here?](https://github.com/users/akyoto/sponsorship)
|
||||
|
||||
[godoc-image]: https://godoc.org/github.com/akyoto/cache?status.svg
|
||||
[godoc-url]: https://godoc.org/github.com/akyoto/cache
|
||||
[report-image]: https://goreportcard.com/badge/github.com/akyoto/cache
|
||||
[report-url]: https://goreportcard.com/report/github.com/akyoto/cache
|
||||
[tests-image]: https://cloud.drone.io/api/badges/akyoto/cache/status.svg
|
||||
[tests-url]: https://cloud.drone.io/akyoto/cache
|
||||
[coverage-image]: https://codecov.io/gh/akyoto/cache/graph/badge.svg
|
||||
[coverage-url]: https://codecov.io/gh/akyoto/cache
|
||||
[sponsor-image]: https://img.shields.io/badge/github-donate-green.svg
|
||||
[sponsor-url]: https://github.com/users/akyoto/sponsorship
|
||||
37
vendor/github.com/akyoto/cache/README.src.md
generated
vendored
Normal file
37
vendor/github.com/akyoto/cache/README.src.md
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
# {name}
|
||||
|
||||
{go:header}
|
||||
|
||||
Cache arbitrary data with an expiration time.
|
||||
|
||||
## Features
|
||||
|
||||
* 0 dependencies
|
||||
* About 100 lines of code
|
||||
* 100% test coverage
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
// New cache
|
||||
c := cache.New(5 * time.Minute)
|
||||
|
||||
// Put something into the cache
|
||||
c.Set("a", "b", 1 * time.Minute)
|
||||
|
||||
// Read from the cache
|
||||
obj, found := c.Get("a")
|
||||
|
||||
// Convert the type
|
||||
fmt.Println(obj.(string))
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
||||
```text
|
||||
BenchmarkGet-12 300000000 3.88 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkSet-12 10000000 183 ns/op 48 B/op 2 allocs/op
|
||||
BenchmarkNew-12 10000000 112 ns/op 352 B/op 5 allocs/op
|
||||
```
|
||||
|
||||
{go:footer}
|
||||
Reference in New Issue
Block a user