Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created March 29, 2019 05:19
Show Gist options
  • Save danielplawgo/7274afaa6485675f991a768fa9188f16 to your computer and use it in GitHub Desktop.
Save danielplawgo/7274afaa6485675f991a768fa9188f16 to your computer and use it in GitHub Desktop.
Cache oraz Future z EF Plus
public class CacheDemo
{
public void Run()
{
WithCache();
}
private void WithCache()
{
using (var db = new DataContext())
{
var categories = db.Categories.Where(c => c.IsActive).FromCache().ToList();
Console.WriteLine($"Categories count: {categories.Count}");
categories = db.Categories.Where(c => c.IsActive).FromCache().ToList();
Console.WriteLine($"Categories count: {categories.Count}");
}
}
}
public class FutureDemo
{
public void Run()
{
WithFuture();
}
private void WithFuture()
{
using (var db = new DataContext())
{
var categoryQuery = db.Categories.Where(c => c.Id == 1).Future();
var productQuery = db.Products.Where(c => c.Id == 1).Future();
var category = categoryQuery.FirstOrDefault();
var product = productQuery.FirstOrDefault();
Console.WriteLine($"Category: {category.Name}, Product: {product.Name}");
}
}
}
public class CacheDemo
{
public void Run()
{
WithoutCache();
}
private void WithoutCache()
{
using (var db = new DataContext())
{
var categories = db.Categories.Where(c => c.IsActive).ToList();
Console.WriteLine($"Categories count: {categories.Count}");
categories = db.Categories.Where(c => c.IsActive).ToList();
Console.WriteLine($"Categories count: {categories.Count}");
}
}
}
SELECT [Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[Categories] AS [Extent1]
WHERE [Extent1].[IsActive] = 1
public class FutureDemo
{
public void Run()
{
WithoutFuture();
}
private void WithoutFuture()
{
using (var db = new DataContext())
{
var categoryQuery = db.Categories.Where(c => c.Id == 1);
var productQuery = db.Products.Where(c => c.Id == 1);
var category = categoryQuery.FirstOrDefault();
var product = productQuery.FirstOrDefault();
Console.WriteLine($"Category: {category.Name}, Product: {product.Name}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment