Skip to content

Commit 028ab40

Browse files
committed
Added full API to REAME
1 parent a18edfe commit 028ab40

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,86 @@ FastCache uses `Environment.TickCount` to monitor items' TTL. `Environment.TickC
8989
The above is no longer valid, we have switched to .NET 6 targeting and now use `TickCount64` which is free of this problem.
9090

9191
Another tradeoff: MemoryCache watches memory usage, and evicts items once it senses memory pressure. **FastCache does not do any of that** it is up to you to keep your caches reasonably sized. After all, it's just a dictionary.
92+
93+
## API Reference
94+
95+
### `FastCache<TKey, TValue>`
96+
97+
Implements `IEnumerable<KeyValuePair<TKey, TValue>>`, `IDisposable`.
98+
99+
#### Constructor
100+
101+
```csharp
102+
FastCache(int cleanupJobInterval = 10000, EvictionCallback itemEvicted = null)
103+
```
104+
105+
Creates a new empty cache instance.
106+
107+
| Parameter | Type | Default | Description |
108+
|-----------|------|---------|-------------|
109+
| `cleanupJobInterval` | `int` | `10000` | Background cleanup interval in milliseconds |
110+
| `itemEvicted` | `EvictionCallback` | `null` | Optional callback when an item is evicted (runs on thread pool) |
111+
112+
#### Methods
113+
114+
##### `AddOrUpdate(TKey key, TValue value, TimeSpan ttl)`
115+
116+
Adds an item to cache or updates it if it already exists. Updating resets the TTL (sliding expiration).
117+
118+
##### `AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory, TimeSpan ttl)`
119+
120+
Factory overload. Uses `addValueFactory` when the key is new, `updateValueFactory` when it exists.
121+
122+
##### `TryGet(TKey key, out TValue value)``bool`
123+
124+
Attempts to get a value by key. Returns `true` if found and not expired.
125+
126+
##### `TryAdd(TKey key, TValue value, TimeSpan ttl)``bool`
127+
128+
Attempts to add a key/value item. Returns `false` if the key already exists (and is not expired).
129+
130+
##### `GetOrAdd(TKey key, Func<TKey, TValue> valueFactory, TimeSpan ttl)``TValue`
131+
132+
Returns existing value if cached, otherwise calls the factory to create, cache, and return it.
133+
134+
##### `GetOrAdd(TKey key, TValue value, TimeSpan ttl)``TValue`
135+
136+
Returns existing value if cached, otherwise adds the provided value and returns it.
137+
138+
##### `GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TimeSpan ttl, TArg factoryArgument)``TValue`
139+
140+
Same as `GetOrAdd` but accepts a `factoryArgument` to avoid closure allocations.
141+
142+
##### `Touch(TKey key, TimeSpan ttl)`
143+
144+
Resets the TTL for an existing (non-expired) item — sliding expiration.
145+
146+
##### `Remove(TKey key)`
147+
148+
Removes the item with the specified key.
149+
150+
##### `TryRemove(TKey key, out TValue value)``bool`
151+
152+
Removes the item and returns the removed value. Returns `false` if not found or expired.
153+
154+
##### `EvictExpired()`
155+
156+
Manually triggers cleanup of expired items. Rarely needed since `TryGet` checks TTL anyway.
157+
158+
#### Properties
159+
160+
| Property | Type | Description |
161+
|----------|------|-------------|
162+
| `Count` | `int` | Total item count, including expired items not yet cleaned up |
163+
164+
#### `Clear()`
165+
166+
Removes all items from the cache.
167+
168+
#### Delegate
169+
170+
```csharp
171+
delegate void EvictionCallback(TKey key)
172+
```
173+
174+
Callback invoked (on thread pool) when an item is evicted from the cache.

0 commit comments

Comments
 (0)