Created
August 27, 2018 13:01
-
-
Save danielplawgo/7c2924fe80b035c59c05001fc0e09f82 to your computer and use it in GitHub Desktop.
Jak cachować dane w .NET? Kilka słów o CacheManager oraz Redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CacheService : ICacheService | |
{ | |
private ICacheManager<object> _cache; | |
private const string _defaultCacheName = "defaultCache"; | |
public CacheService() | |
{ | |
var builder = ConfigurationBuilder.LoadConfiguration(_defaultCacheName).Builder; | |
builder.WithMicrosoftLogging(s => s.AddNLog()); | |
_cache = CacheFactory.FromConfiguration<object>(_defaultCacheName, builder.Build()); | |
} | |
public T GetOrAdd<T>(string key, Func<T> getFunc) | |
{ | |
return (T)_cache.GetOrAdd(key, k => getFunc()); | |
} | |
public T GetOrAdd<T>(string key, string region, Func<T> getFunc) | |
{ | |
return (T)_cache.GetOrAdd(key, region, (k, r) => getFunc()); | |
} | |
public void Remove(string key) | |
{ | |
_cache.Remove(key); | |
} | |
public void Remove(string key, string region) | |
{ | |
_cache.Remove(key, region); | |
} | |
public void Clear() | |
{ | |
_cache.Clear(); | |
} | |
public void ClearRegion(string region) | |
{ | |
_cache.ClearRegion(region); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ICacheService | |
{ | |
T GetOrAdd<T>(string key, Func<T> getFunc); | |
T GetOrAdd<T>(string key, string region, Func<T> getFunc); | |
void Remove(string key); | |
void Remove(string key, string region); | |
void Clear(); | |
void ClearRegion(string region); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductLogic : IProductLogic | |
{ | |
private Lazy<IProductRepository> _repository; | |
protected IProductRepository Repository | |
{ | |
get { return _repository.Value; } | |
} | |
private Lazy<ICacheService> _cacheService; | |
protected ICacheService CacheService | |
{ | |
get { return _cacheService.Value; } | |
} | |
public ProductLogic(Lazy<IProductRepository> repository, | |
Lazy<ICacheService> cacheService) | |
{ | |
_repository = repository; | |
_cacheService = cacheService; | |
} | |
public IEnumerable<Product> GetAll() | |
{ | |
return Repository.GetAll(); | |
} | |
public Product GetById(int id) | |
{ | |
return CacheService.GetOrAdd(CacheKey(id), () => Repository.GetById(id)); | |
} | |
public Product Update(Product product) | |
{ | |
Repository.Update(product); | |
CacheService.Remove(CacheKey(product.Id)); | |
return product; | |
} | |
public void Delete(Product product) | |
{ | |
Repository.Delete(product); | |
CacheService.Remove(CacheKey(product.Id)); | |
} | |
private string CacheKey(int id) | |
{ | |
return $"Product-{id}"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductRepository : IProductRepository | |
{ | |
private Lazy<DataContext> _db; | |
protected DataContext Db | |
{ | |
get { return _db.Value; } | |
} | |
public ProductRepository(Lazy<DataContext> db) | |
{ | |
_db = db; | |
} | |
public IEnumerable<Product> GetAll() | |
{ | |
return Db.Products; | |
} | |
public Product GetById(int id) | |
{ | |
return Db.Products.FirstOrDefault(p => p.Id == id); | |
} | |
public void Update(Product product) | |
{ | |
Db.Products.Attach(product); | |
Db.Entry(product).State = EntityState.Modified; | |
Db.SaveChanges(); | |
} | |
public void Delete(Product product) | |
{ | |
Db.Products.Attach(product); | |
Db.Products.Remove(product); | |
Db.SaveChanges(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cacheManager.Redis> | |
<connections> | |
<connection id="redis" | |
database="0" | |
password="password" | |
ssl="true"> | |
<endpoints> | |
<endpoint host="host" | |
port="port" /> | |
</endpoints> | |
</connection> | |
</connections> | |
</cacheManager.Redis> | |
<cacheManager xmlns="http://cachemanager.michaco.net/schemas/CacheManagerCfg.xsd"> | |
<managers> | |
<cache name="defaultCache" | |
enableStatistics="false" | |
enablePerformanceCounters="false" | |
backplaneName="redis" | |
backplaneType="CacheManager.Redis.RedisCacheBackplane, CacheManager.StackExchange.Redis" | |
serializerType="CacheManager.Serialization.Json.JsonCacheSerializer, CacheManager.Serialization.Json"> | |
<handle name="handleName" | |
ref="systemRuntimeHandle" | |
expirationMode="Absolute" | |
timeout="1m" /> | |
<handle name="redis" | |
ref="redisHandle" | |
expirationMode="Sliding" | |
timeout="10m" | |
isBackplaneSource="true" /> | |
</cache> | |
</managers> | |
<cacheHandles> | |
<handleDef id="systemRuntimeHandle" | |
type="CacheManager.SystemRuntimeCaching.MemoryCacheHandle`1, CacheManager.SystemRuntimeCaching" /> | |
<handleDef id="redisHandle" | |
type="CacheManager.Redis.RedisCacheHandle`1, CacheManager.StackExchange.Redis" /> | |
</cacheHandles> | |
</cacheManager> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment