Skip to content

Commit 527204e

Browse files
authored
Merge pull request #3 from PraserX/readme-update
Readme, Github actions
2 parents 6e6267e + b06b6a9 commit 527204e

6 files changed

Lines changed: 158 additions & 24 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Package atomiccache test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
strategy:
6+
matrix:
7+
go-version: [1.16.x, 1.17.x]
8+
os: [ubuntu-latest, macos-latest, windows-latest]
9+
runs-on: ${{ matrix.os }}
10+
steps:
11+
- name: Install Go
12+
uses: actions/setup-go@v2
13+
with:
14+
go-version: ${{ matrix.go-version }}
15+
- name: Checkout code
16+
uses: actions/checkout@v2
17+
- name: Install staticcheck
18+
run: go install honnef.co/go/tools/cmd/staticcheck@latest
19+
- name: Test
20+
run: go test ./...
21+
- name: Staticcheck test
22+
run: staticcheck ./...
23+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ master ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ master ]
20+
schedule:
21+
- cron: '33 15 * * 6'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'go' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v2
42+
43+
# Initializes the CodeQL tools for scanning.
44+
- name: Initialize CodeQL
45+
uses: github/codeql-action/init@v1
46+
with:
47+
languages: ${{ matrix.language }}
48+
# If you wish to specify custom queries, you can do so here or in a config file.
49+
# By default, queries listed here will override any specified in a config file.
50+
# Prefix the list here with "+" to use these queries and those in the config file.
51+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
52+
53+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54+
# If this step fails, then you should remove it and run the build manually (see below)
55+
- name: Autobuild
56+
uses: github/codeql-action/autobuild@v1
57+
58+
# ℹ️ Command-line programs to run using the OS shell.
59+
# 📚 https://git.io/JvXDl
60+
61+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62+
# and modify them (or add more) to build your code if your project
63+
# uses a compiled language
64+
65+
#- run: |
66+
# make bootstrap
67+
# make release
68+
69+
- name: Perform CodeQL Analysis
70+
uses: github/codeql-action/analyze@v1

.travis.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

README.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
11
# Atomic Cache
22

3-
Atomic cache is Golang fast in-memory cache (it wants to be fast - if you want to help, go ahead). Cache using limited nubmer of shards with limited number of containing records. So the memory is limited, but the limit depends on you.
3+
Atomic cache is Golang fast in-memory cache (it wants to be fast - if you want to help, go ahead). Cache using limited number of shards with limited number of containing records. So the memory is limited, but the limit depends on you.
44

55
After cache initialization, only one shard is allocated. After that, if there is no left space in shard, new one is allocated. If shard is empty, memory is freed.
66

77
There is also support for record expiration. You can set expire time for every record in cache memory.
88

9+
## Configuration
10+
11+
| Option | Type | Description |
12+
| ---------------- | ------ | ---------------------------------------------------------------------- |
13+
| RecordSizeSmall | int | Size of byte array used for memory allocation at small shard section. |
14+
| RecordSizeMedium | int | Size of byte array used for memory allocation at medium shard section. |
15+
| RecordSizeLarge | int | Size of byte array used for memory allocation at large shard section. |
16+
| MaxRecords | int | Maximum records per shard. |
17+
| MaxShardsSmall | int | Maximum small shards which can be allocated in cache memory. |
18+
| MaxShardsMedium | int | Maximum medium shards which can be allocated in cache memory. |
19+
| MaxShardsLarge | int | Maximum large shards which can be allocated in cache memory. |
20+
| GcStarter | uint32 | Garbage collector starter (run garbage collection every X sets). |
21+
922
## Example usage
1023

1124
```go
1225
// Initialize cache memory (ac == atomiccache)
1326
cache := ac.New(OptionMaxRecords(512), OptionRecordSize(2048), OptionMaxShards(48))
1427

15-
// Store data in cache memory
16-
cache.Set([]byte("key"), []byte("data"), 500*time.Millisecond)
28+
// Store data in cache memory - key, data, record valid time
29+
cache.Set("key", []byte("data"), 500*time.Millisecond)
1730

1831
// Get data from cache memory
19-
if _, err := cache.Get([]byte("key")); err != nil {
32+
if _, err := cache.Get("key"); err != nil {
2033
fmt.Fprintf(os.Stderr, "Cache is empty, but expecting some data: %v", err)
2134
os.Exit(1)
2235
}
2336
```
2437

2538
## Benchmark
2639

27-
For this benchmark was created memory with following specs: `2048 bytes per record`, `2048 records per shard`, `128 shards (max)`. The 2048 bytes was set.
40+
For this benchmark was created memory with following specs: `1024 bytes per record`, `4096 records per shard`, `256 shards (max)`. The 1024 bytes was set.
2841

2942
```
30-
BenchmarkCacheNewMedium-12 314 3804726 ns/op 22686418 B/op 12402 allocs/op
31-
BenchmarkCacheSetMedium-12 1845970 664.2 ns/op 129 B/op 5 allocs/op
32-
BenchmarkCacheGetMedium-12 5701435 209.1 ns/op 16 B/op 1 allocs/op
43+
BenchmarkCacheNewMedium-12 291 3670372 ns/op 22776481 B/op 12408 allocs/op
44+
BenchmarkCacheSetMedium-12 1928548 620.3 ns/op 63 B/op 1 allocs/op
45+
BenchmarkCacheGetMedium-12 16707145 69.87 ns/op 0 B/op 0 allocs/op
3346
```
3447

3548
*If you want do some special bencharking, go ahead.*
@@ -38,17 +51,16 @@ BenchmarkCacheGetMedium-12 5701435 209.1 ns/op 16 B/op
3851

3952
**SET**
4053
```
41-
BenchmarkAtomicCacheSet-12 2402160 519.6 ns/op 153 B/op 6 allocs/op
42-
BenchmarkBigCacheSet-12 4021484 376.7 ns/op 66 B/op 1 allocs/op
43-
BenchmarkFreeCacheSet-12 10443246 100.8 ns/op 0 B/op 0 allocs/op
44-
BenchmarkHashicorpCacheSet-12 2490759 498.4 ns/op 150 B/op 5 allocs/op
54+
BenchmarkAtomicCacheSet-12 2921170 413.0 ns/op 55 B/op 2 allocs/op
55+
BenchmarkBigCacheSet-12 3448020 345.5 ns/op 0 B/op 0 allocs/op
56+
BenchmarkFreeCacheSet-12 4777364 217.2 ns/op 65 B/op 1 allocs/op
57+
BenchmarkHashicorpCacheSet-12 6208528 202.2 ns/op 65 B/op 3 allocs/op
4558
```
4659

4760
**GET**
4861
```
49-
BenchmarkAtomicCacheGet-12 6300744 187.6 ns/op 16 B/op 1 allocs/op
50-
BenchmarkBigCacheGet-12 3798052 322.5 ns/op 37 B/op 2 allocs/op
51-
BenchmarkFreeCacheGet-12 9000188 127.0 ns/op 24 B/op 1 allocs/op
52-
BenchmarkHashicorpCacheGet-12 3039728 405.8 ns/op 7 B/op 0 allocs/op
53-
62+
BenchmarkAtomicCacheGet-12 9697010 121.7 ns/op 0 B/op 0 allocs/op
63+
BenchmarkBigCacheGet-12 4031352 295.3 ns/op 88 B/op 2 allocs/op
64+
BenchmarkFreeCacheGet-12 4813386 276.8 ns/op 88 B/op 2 allocs/op
65+
BenchmarkHashicorpCacheGet-12 11071472 107.4 ns/op 16 B/op 1 allocs/op
5466
```

go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/PraserX/atomic-cache
2+
3+
go 1.17
4+
5+
require (
6+
github.com/allegro/bigcache v1.2.1
7+
github.com/coocood/freecache v1.1.1
8+
github.com/hashicorp/golang-lru v0.5.4
9+
)
10+
11+
require (
12+
github.com/cespare/xxhash v1.1.0 // indirect
13+
github.com/stretchr/testify v1.7.0 // indirect
14+
)

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
2+
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
3+
github.com/allegro/bigcache v1.2.1 h1:hg1sY1raCwic3Vnsvje6TT7/pnZba83LeFck5NrFKSc=
4+
github.com/allegro/bigcache v1.2.1/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
5+
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
6+
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
7+
github.com/coocood/freecache v1.1.1 h1:uukNF7QKCZEdZ9gAV7WQzvh0SbjwdMF6m3x3rxEkaPc=
8+
github.com/coocood/freecache v1.1.1/go.mod h1:OKrEjkGVoxZhyWAJoeFi5BMLUJm2Tit0kpGkIr7NGYY=
9+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
10+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
12+
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
13+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
14+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
16+
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
17+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
19+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
22+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)