Skip to content

Commit b1c1d32

Browse files
committed
feat(adapter): initial W-TinyLFU cache adapter implementation
Local cache adapter porting Caffeine's Window TinyLFU admission policy to Elixir. Combines recency (LRU) and frequency (4-bit Count-Min Sketch) for near-optimal hit rates with a lock-free hot path. See the moduledoc and README for architecture and configuration.
1 parent 054f863 commit b1c1d32

26 files changed

Lines changed: 5766 additions & 25 deletions

‎.credo.exs‎

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# This file contains the configuration for Credo and you are probably reading
2+
# this after creating it with `mix credo.gen.config`.
3+
#
4+
# If you find anything wrong or unclear in this file, please report an
5+
# issue on GitHub: https://github.com/rrrene/credo/issues
6+
#
7+
%{
8+
#
9+
# You can have as many configs as you like in the `configs:` field.
10+
configs: [
11+
%{
12+
#
13+
# Run any config using `mix credo -C <name>`. If no config name is given
14+
# "default" is used.
15+
#
16+
name: "default",
17+
#
18+
# These are the files included in the analysis:
19+
files: %{
20+
#
21+
# You can give explicit globs or simply directories.
22+
# In the latter case `**/*.{ex,exs}` will be used.
23+
#
24+
included: [
25+
"lib/",
26+
"src/",
27+
"test/",
28+
"web/",
29+
"apps/*/lib/",
30+
"apps/*/src/",
31+
"apps/*/test/",
32+
"apps/*/web/"
33+
],
34+
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
35+
},
36+
#
37+
# Load and configure plugins here:
38+
#
39+
plugins: [],
40+
#
41+
# If you create your own checks, you must specify the source files for
42+
# them here, so they can be loaded by Credo before running the analysis.
43+
#
44+
requires: [],
45+
#
46+
# If you want to enforce a style guide and need a more traditional linting
47+
# experience, you can change `strict` to `true` below:
48+
#
49+
strict: false,
50+
#
51+
# To modify the timeout for parsing files, change this value:
52+
#
53+
parse_timeout: 5000,
54+
#
55+
# If you want to use uncolored output by default, you can change `color`
56+
# to `false` below:
57+
#
58+
color: true,
59+
#
60+
# You can customize the parameters of any check by adding a second element
61+
# to the tuple.
62+
#
63+
# To disable a check put `false` as second element:
64+
#
65+
# {Credo.Check.Design.DuplicatedCode, false}
66+
#
67+
checks: %{
68+
enabled: [
69+
#
70+
## Consistency Checks
71+
#
72+
{Credo.Check.Consistency.ExceptionNames, []},
73+
{Credo.Check.Consistency.LineEndings, []},
74+
{Credo.Check.Consistency.ParameterPatternMatching, []},
75+
{Credo.Check.Consistency.SpaceAroundOperators, []},
76+
{Credo.Check.Consistency.SpaceInParentheses, []},
77+
{Credo.Check.Consistency.TabsOrSpaces, []},
78+
79+
#
80+
## Design Checks
81+
#
82+
# You can customize the priority of any check
83+
# Priority values are: `low, normal, high, higher`
84+
#
85+
{Credo.Check.Design.AliasUsage,
86+
[priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
87+
{Credo.Check.Design.TagFIXME, []},
88+
# You can also customize the exit_status of each check.
89+
# If you don't want TODO comments to cause `mix credo` to fail, just
90+
# set this value to 0 (zero).
91+
#
92+
{Credo.Check.Design.TagTODO, [exit_status: 0]},
93+
{Credo.Check.Design.TagFIXME, [exit_status: 0]},
94+
95+
#
96+
## Readability Checks
97+
#
98+
{Credo.Check.Readability.AliasOrder, []},
99+
{Credo.Check.Readability.FunctionNames, []},
100+
{Credo.Check.Readability.LargeNumbers, []},
101+
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
102+
{Credo.Check.Readability.ModuleAttributeNames, []},
103+
{Credo.Check.Readability.ModuleDoc, []},
104+
{Credo.Check.Readability.ModuleNames, []},
105+
{Credo.Check.Readability.ParenthesesInCondition, []},
106+
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
107+
{Credo.Check.Readability.PipeIntoAnonymousFunctions, []},
108+
{Credo.Check.Readability.PredicateFunctionNames, []},
109+
{Credo.Check.Readability.PreferImplicitTry, []},
110+
{Credo.Check.Readability.RedundantBlankLines, []},
111+
{Credo.Check.Readability.Semicolons, []},
112+
{Credo.Check.Readability.SpaceAfterCommas, []},
113+
{Credo.Check.Readability.StringSigils, []},
114+
{Credo.Check.Readability.TrailingBlankLine, []},
115+
{Credo.Check.Readability.TrailingWhiteSpace, []},
116+
{Credo.Check.Readability.UnnecessaryAliasExpansion, []},
117+
{Credo.Check.Readability.VariableNames, []},
118+
{Credo.Check.Readability.WithSingleClause, []},
119+
120+
#
121+
## Refactoring Opportunities
122+
#
123+
{Credo.Check.Refactor.Apply, []},
124+
{Credo.Check.Refactor.CondStatements, []},
125+
{Credo.Check.Refactor.CyclomaticComplexity, []},
126+
{Credo.Check.Refactor.FilterCount, []},
127+
{Credo.Check.Refactor.FilterFilter, []},
128+
{Credo.Check.Refactor.FunctionArity, [max_arity: 10]},
129+
{Credo.Check.Refactor.LongQuoteBlocks, []},
130+
{Credo.Check.Refactor.MapJoin, []},
131+
{Credo.Check.Refactor.MatchInCondition, []},
132+
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
133+
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
134+
{Credo.Check.Refactor.Nesting, []},
135+
{Credo.Check.Refactor.RedundantWithClauseResult, []},
136+
{Credo.Check.Refactor.RejectReject, []},
137+
{Credo.Check.Refactor.UnlessWithElse, []},
138+
{Credo.Check.Refactor.WithClauses, []},
139+
140+
#
141+
## Warnings
142+
#
143+
{Credo.Check.Warning.ApplicationConfigInModuleAttribute, []},
144+
{Credo.Check.Warning.BoolOperationOnSameValues, []},
145+
{Credo.Check.Warning.Dbg, []},
146+
{Credo.Check.Warning.ExpensiveEmptyEnumCheck, []},
147+
{Credo.Check.Warning.IExPry, []},
148+
{Credo.Check.Warning.IoInspect, []},
149+
{Credo.Check.Warning.MissedMetadataKeyInLoggerConfig, []},
150+
{Credo.Check.Warning.OperationOnSameValues, []},
151+
{Credo.Check.Warning.OperationWithConstantResult, []},
152+
{Credo.Check.Warning.RaiseInsideRescue, []},
153+
{Credo.Check.Warning.SpecWithStruct, []},
154+
{Credo.Check.Warning.StructFieldAmount, []},
155+
{Credo.Check.Warning.UnsafeExec, []},
156+
{Credo.Check.Warning.UnusedEnumOperation, []},
157+
{Credo.Check.Warning.UnusedFileOperation, []},
158+
{Credo.Check.Warning.UnusedKeywordOperation, []},
159+
{Credo.Check.Warning.UnusedListOperation, []},
160+
{Credo.Check.Warning.UnusedMapOperation, []},
161+
{Credo.Check.Warning.UnusedPathOperation, []},
162+
{Credo.Check.Warning.UnusedRegexOperation, []},
163+
{Credo.Check.Warning.UnusedStringOperation, []},
164+
{Credo.Check.Warning.UnusedTupleOperation, []},
165+
{Credo.Check.Warning.WrongTestFilename, []}
166+
],
167+
disabled: [
168+
#
169+
# Checks scheduled for next check update (opt-in for now)
170+
{Credo.Check.Refactor.UtcNowTruncate, []},
171+
172+
#
173+
# Controversial and experimental checks (opt-in, just move the check to `:enabled`
174+
# and be sure to use `mix credo --strict` to see low priority checks)
175+
#
176+
{Credo.Check.Consistency.MultiAliasImportRequireUse, []},
177+
{Credo.Check.Consistency.UnusedVariableNames, []},
178+
{Credo.Check.Design.DuplicatedCode, []},
179+
{Credo.Check.Design.SkipTestWithoutComment, []},
180+
{Credo.Check.Readability.AliasAs, []},
181+
{Credo.Check.Readability.BlockPipe, []},
182+
{Credo.Check.Readability.ImplTrue, []},
183+
{Credo.Check.Readability.MultiAlias, []},
184+
{Credo.Check.Readability.NestedFunctionCalls, []},
185+
{Credo.Check.Readability.OneArityFunctionInPipe, []},
186+
{Credo.Check.Readability.OnePipePerLine, []},
187+
{Credo.Check.Readability.SeparateAliasRequire, []},
188+
{Credo.Check.Readability.SingleFunctionToBlockPipe, []},
189+
{Credo.Check.Readability.SinglePipe, []},
190+
{Credo.Check.Readability.Specs, []},
191+
{Credo.Check.Readability.StrictModuleLayout, []},
192+
{Credo.Check.Readability.WithCustomTaggedTuple, []},
193+
{Credo.Check.Refactor.ABCSize, []},
194+
{Credo.Check.Refactor.AppendSingleItem, []},
195+
{Credo.Check.Refactor.CondInsteadOfIfElse, []},
196+
{Credo.Check.Refactor.DoubleBooleanNegation, []},
197+
{Credo.Check.Refactor.FilterReject, []},
198+
{Credo.Check.Refactor.IoPuts, []},
199+
{Credo.Check.Refactor.MapMap, []},
200+
{Credo.Check.Refactor.ModuleDependencies, []},
201+
{Credo.Check.Refactor.NegatedIsNil, []},
202+
{Credo.Check.Refactor.PassAsyncInTestCases, []},
203+
{Credo.Check.Refactor.PipeChainStart, []},
204+
{Credo.Check.Refactor.RejectFilter, []},
205+
{Credo.Check.Refactor.VariableRebinding, []},
206+
{Credo.Check.Warning.LazyLogging, []},
207+
{Credo.Check.Warning.LeakyEnvironment, []},
208+
{Credo.Check.Warning.MapGetUnsafePass, []},
209+
{Credo.Check.Warning.MixEnv, []},
210+
{Credo.Check.Warning.UnsafeToAtom, []}
211+
# {Credo.Check.Warning.UnusedOperation, [{MyMagicModule, [:fun1, :fun2]}]}
212+
213+
# {Credo.Check.Refactor.MapInto, []},
214+
215+
#
216+
# Custom checks can be created using `mix credo.gen.check`.
217+
#
218+
]
219+
}
220+
}
221+
]
222+
}

‎.formatter.exs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Used by "mix format"
2+
[
3+
import_deps: [:nebulex],
4+
inputs: ["{mix,.formatter}.exs", "{config,lib,test,benchmarks}/**/*.{ex,exs}"],
5+
line_length: 100
6+
]

‎.github/workflows/ci.yml‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
nebulex_test:
13+
name: >-
14+
Nebulex TinyLFU Test (Elixir ${{ matrix.elixir }} /
15+
OTP ${{ matrix.otp }} /
16+
OS ${{ matrix.os }})
17+
runs-on: ${{ matrix.os }}
18+
19+
# Minimum OTP is 26 because `Nebulex.TinyLFU.AccessOrderDeque` uses
20+
# `:ets.first_lookup/1`, added in OTP 26.0. Elixir minimum is 1.15
21+
# (the first release supporting OTP 26).
22+
strategy:
23+
matrix:
24+
include:
25+
- elixir: 1.19.x
26+
otp: 28.x
27+
os: 'ubuntu-latest'
28+
lint: true
29+
coverage: true
30+
sobelow: true
31+
dialyzer: true
32+
- elixir: 1.18.x
33+
otp: 28.x
34+
os: 'ubuntu-latest'
35+
- elixir: 1.17.x
36+
otp: 27.x
37+
os: 'ubuntu-latest'
38+
- elixir: 1.15.x
39+
otp: 26.x
40+
os: 'ubuntu-latest'
41+
42+
env:
43+
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
44+
CODECOV_TOKEN: '${{ secrets.CODECOV_TOKEN }}'
45+
MIX_ENV: test
46+
NEBULEX_PATH: nebulex
47+
48+
steps:
49+
- uses: actions/checkout@v4
50+
51+
- name: Install OTP and Elixir
52+
uses: erlef/setup-beam@v1
53+
with:
54+
otp-version: '${{ matrix.otp }}'
55+
elixir-version: '${{ matrix.elixir }}'
56+
57+
- name: Cache deps
58+
uses: actions/cache@v4
59+
id: mix-cache
60+
with:
61+
path: deps
62+
key: >-
63+
${{ matrix.elixir }}-${{ matrix.otp }}-${{ runner.os }}-mix-${{
64+
hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
65+
restore-keys: |
66+
${{ matrix.elixir }}-${{ matrix.otp }}-${{ runner.os }}-mix-
67+
68+
- name: Cache _build
69+
uses: actions/cache@v4
70+
with:
71+
path: _build
72+
key: >-
73+
${{ matrix.elixir }}-${{ matrix.otp }}-${{ runner.os }}-build-${{
74+
hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
75+
restore-keys: |
76+
${{ matrix.elixir }}-${{ matrix.otp }}-${{ runner.os }}-build-
77+
78+
- name: Install Hex and Rebar
79+
run: |
80+
mix local.hex --force
81+
mix local.rebar --force
82+
83+
- name: Install Nebulex
84+
run: mix nbx.setup
85+
86+
- name: Install Dependencies
87+
run: mix deps.get
88+
if: ${{ steps.mix-cache.outputs.cache-hit != 'true' }}
89+
90+
- name: Compile deps
91+
run: mix deps.compile
92+
93+
- name: Check unused dependencies
94+
run: mix deps.unlock --check-unused
95+
if: ${{ matrix.lint }}
96+
97+
- name: Compile lint
98+
run: mix compile --warnings-as-errors
99+
if: ${{ matrix.lint }}
100+
101+
- name: Run style and code consistency checks
102+
run: |
103+
mix format --check-formatted
104+
mix credo --strict
105+
if: ${{ matrix.lint }}
106+
107+
- name: Run tests
108+
run: mix test
109+
if: ${{ !matrix.coverage }}
110+
111+
- name: Run tests with coverage
112+
run: mix coveralls.json
113+
if: ${{ matrix.coverage }}
114+
115+
- name: Upload coverage reports to Codecov
116+
uses: codecov/codecov-action@v5
117+
with:
118+
fail_ci_if_error: true
119+
flags: unittests-elixir-${{ matrix.elixir }}-otp-${{ matrix.otp }}
120+
if: ${{ matrix.coverage }}
121+
122+
- name: Run sobelow
123+
run: mix sobelow --skip --exit Low
124+
if: ${{ matrix.sobelow }}
125+
126+
- name: Restore PLT Cache
127+
uses: actions/cache@v4
128+
id: plt-cache
129+
with:
130+
path: priv/plts
131+
key: >-
132+
plt-${{ matrix.elixir }}-${{ matrix.otp }}-${{
133+
hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
134+
restore-keys: |
135+
plt-${{ matrix.elixir }}-${{ matrix.otp }}-
136+
if: ${{ matrix.dialyzer }}
137+
138+
- name: Create PLTs
139+
run: |
140+
mkdir -p priv/plts
141+
mix dialyzer --plt
142+
if: ${{ matrix.dialyzer && steps.plt-cache.outputs.cache-hit != 'true' }}
143+
144+
- name: Run dialyzer
145+
run: mix dialyzer --format github
146+
if: ${{ matrix.dialyzer }}

0 commit comments

Comments
 (0)