CacheValue.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using StackExchange.Redis;
  3. namespace VCommon.Caching
  4. {
  5. public struct CacheValue
  6. {
  7. private RedisValue _redisValue;
  8. public static implicit operator CacheValue(RedisValue value) => new CacheValue { _redisValue = value };
  9. public static implicit operator RedisValue(CacheValue self) => self._redisValue;
  10. public static explicit operator CacheValue(long value) => new CacheValue { _redisValue = value };
  11. public static explicit operator CacheValue(long? value) => new CacheValue { _redisValue = value };
  12. public static explicit operator CacheValue(double value) => new CacheValue { _redisValue = value };
  13. public static explicit operator CacheValue(double? value) => new CacheValue { _redisValue = value };
  14. public static explicit operator CacheValue(bool value) => new CacheValue { _redisValue = value };
  15. public static explicit operator CacheValue(bool? value) => new CacheValue { _redisValue = value };
  16. public static explicit operator CacheValue(string value) => new CacheValue { _redisValue = value };
  17. public static explicit operator CacheValue(Guid value) => new CacheValue { _redisValue = value.ToString() };
  18. public static explicit operator CacheValue(Guid? value) => new CacheValue { _redisValue = value?.ToString() };
  19. public static explicit operator CacheValue(DateTime value) => new CacheValue { _redisValue = value.Ticks };
  20. public static explicit operator CacheValue(DateTime? value) => new CacheValue { _redisValue = value?.Ticks };
  21. public static explicit operator double(CacheValue self) => (double)self._redisValue;
  22. public static explicit operator double? (CacheValue self) => (double?)self._redisValue;
  23. public static explicit operator long(CacheValue self) => (long)self._redisValue;
  24. public static explicit operator long? (CacheValue self) => (long?)self._redisValue;
  25. public static explicit operator bool(CacheValue self) => (bool)self._redisValue;
  26. public static explicit operator bool? (CacheValue self) => (bool?)self._redisValue;
  27. public static explicit operator string(CacheValue self) => self._redisValue;
  28. public static explicit operator Guid(CacheValue self) => new Guid((string)self._redisValue);
  29. public static explicit operator Guid? (CacheValue self) => self._redisValue.IsNull ? (Guid?)null : new Guid((string)self._redisValue);
  30. public static explicit operator DateTime? (CacheValue self) => self._redisValue.IsNull ? (DateTime?)null : new DateTime((long)self._redisValue);
  31. public static explicit operator DateTime(CacheValue self) => new DateTime((long)self._redisValue);
  32. public bool IsNull => _redisValue.IsNull;
  33. }
  34. }