HashCacheManager.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. namespace VCommon.Caching
  4. {
  5. public class HashCacheManager
  6. {
  7. private readonly string _key;
  8. private readonly FixedCacheManager _underlying;
  9. public HashCacheManager(string server, int db, string key)
  10. {
  11. _key = key;
  12. _underlying = new FixedCacheManager(server, db);
  13. }
  14. public void Clean() => _underlying.KeyDelete(_key);
  15. public bool Exist(string field) => _underlying.HashFieldExist(_key, field);
  16. public bool Delete(string field) => _underlying.HashFieldDelete(_key, field);
  17. public long Delete(params string[] fields) => _underlying.HashFieldDelete(_key, fields);
  18. public CacheValue Fetch(string field, Func<CacheValue> missingCacheValueProvider, CacheExpire expire = null) => _underlying.HashFieldFetch(_key, field, missingCacheValueProvider, expire);
  19. public T FetchJson<T>(string field, Func<T> missingCacheValueProvider, CacheExpire expire = null) => _underlying.HashFieldFetchJson(_key, field, missingCacheValueProvider, expire);
  20. public void SetJson<T>(string field, T value, CacheExpire expire = null) => _underlying.HashFieldSetJson(_key, field, value, expire);
  21. public T GetJson<T>(string field) => _underlying.HashFieldGetJson<T>(_key, field);
  22. public void PurgeAndSet(IReadOnlyDictionary<string, CacheValue> fields) => _underlying.HashFieldPurgeAndSet(_key, fields);
  23. public void DeleteByPattern(string pattern) => _underlying.HashFieldDeleteByPattern(_key, pattern);
  24. public IReadOnlyDictionary<string, CacheValue> GetByPattern(string pattern) => _underlying.HashFieldGetByPattern(_key, pattern);
  25. public IReadOnlyDictionary<string, T> GetByPatternJson<T>(string pattern) => _underlying.HashFieldGetByPatternJson<T>(_key, pattern);
  26. public void Set(string field, CacheValue value, CacheExpire expire = null) => _underlying.HashFieldSet(_key, field, value, expire);
  27. public CacheValue Get(string field) => _underlying.HashFieldGet(_key, field);
  28. public void Set(IReadOnlyDictionary<string, CacheValue> field, CacheExpire expire = null) => _underlying.HashFieldSet(_key, field, expire);
  29. public long Increment(string field) => _underlying.HashFieldIncrement(_key, field);
  30. public long Decrement(string field) => _underlying.HashFieldDecrement(_key, field);
  31. public IReadOnlyDictionary<string, CacheValue> Get(params string[] fields) => _underlying.HashFieldGet(_key, fields);
  32. public IReadOnlyDictionary<TK, TV> FetchAsDic<TK, TV>(
  33. IEnumerable<TK> fields,
  34. Func<TK, string> convertField, Func<IEnumerable<TK>,
  35. IReadOnlyDictionary<TK, TV>> missingCacheValueProvider,
  36. Func<TV, CacheValue> convertValueToCache,
  37. Func<CacheValue, TV> convertCacheToValue,
  38. CacheExpire expire = null)
  39. {
  40. return _underlying.HashFieldsFetchAsDic(_key, fields, convertField, missingCacheValueProvider, convertValueToCache, convertCacheToValue, expire);
  41. }
  42. }
  43. }