Skip to content

Commit f40457c

Browse files
committed
Improve docs
1 parent c4afb50 commit f40457c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,38 @@ You can define rules to skip caching for certain queries based on their command
290290
});
291291
```
292292

293+
Or if you don't want to use the **`NotCacheable()`** extension method, you can use **query tags** to attach comments to your SQL queries directly from your EF Core code. These tags are then included in the generated SQL, allowing you to create custom rules for caching behavior.
294+
295+
To add a query tag, use the **`.TagWith()`** extension method on your EF Core query:
296+
297+
```csharp
298+
var blogs = await context.Blogs
299+
.TagWith("Fetching data")
300+
.Where(b => b.IsActive)
301+
.ToListAsync();
302+
```
303+
304+
The tag, prepended with `--`, will be included in the generated SQL query:
305+
306+
```sql
307+
-- Fetching data
308+
SELECT * FROM Blogs WHERE IsActive = 1;
309+
```
310+
311+
Now you can use query tags to define rules for when to skip caching. This is done by configuring your cache service to check for specific text in the command.
312+
313+
In your `Startup.cs` or `Program.cs` file, configure the cache to skip commands that contain your tag. The `SkipCachingCommands` method accepts a predicate that evaluates the command text.
314+
315+
```csharp
316+
services.AddEFSecondLevelCache(options =>
317+
{
318+
options.SkipCachingCommands(commandText =>
319+
commandText.Contains("-- Fetching data", StringComparison.InvariantCultureIgnoreCase));
320+
});
321+
```
322+
323+
With this configuration, any query tagged with `"Fetching data"` will not be cached, giving you granular control over your caching strategy.
324+
293325
### Skipping Invalidation
294326

295327
In some cases, you may want to prevent a command from invalidating the cache, such as when updating a view counter.

0 commit comments

Comments
 (0)