Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/jitbit/FastCache
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-jitbit committed Sep 22, 2022
2 parents 6e9477f + 195932b commit 677ce7e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 32 deletions.
32 changes: 1 addition & 31 deletions Atomic.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
# Atomicness

When it comes ot atomicness, the biggest challenge is to check "item exists" and "item not expired" in one go.

When an item was "found but is expired" - we need to treat this as "not found" and discard it. For that we either need to use a lock
so that the the three steps "exist? expired? remove!" are performed atomically. Otherwise another tread might chip in,
and ADD a non-expired item with the same key while we're evicting it. And we'll be removing a non-expired key taht was just added

OR instead of using locks we can remove _by key AND by value_. So if another thread has just rushed in
and added another item with the same key - that other item won't be removed.

basically, instead of doing this

```
lock {
exists?
expired?
remove by key!
}
```

we do this

```
exists? (if yes returns the value)
expired?
remove by key AND value
```

If another thread chipped in while we were in the middle of checking if it's expired or not, and recorded a new value - we won't remove it.

Locks suck becasue add extra 50ns to benchmark, so it becomes 110ns instead of 70ns which sucks.
So - no locks then!
The article has been moved [here](https://www.jitbit.com/alexblog/fast-memory-cache/#perf)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FastCache

7x-10x faster alternative to MemoryCache. A high-performance, lighweight (8KB dll) and thread-safe memory cache for .NET.
7x-10x faster alternative to MemoryCache. A high-performance, lighweight (8KB dll) and [thread-safe](Atomic.md) memory cache for .NET.

[![NuGet version](https://badge.fury.io/nu/jitbit.fastcache.svg)](https://badge.fury.io/nu/jitbit.fastcache)
[![.NET](https://github.com/jitbit/FastCache/actions/workflows/dotnet.yml/badge.svg)](https://github.com/jitbit/FastCache/actions/workflows/dotnet.yml)
Expand Down

0 comments on commit 677ce7e

Please sign in to comment.