示例#1
0
        public Task <T> GetAsync <T>(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key MUST have a value");
            }

            var    tcs      = new TaskCompletionSource <T>();
            string filePath = Path.Combine(_folderPath, GenerateStoredKey(key, typeof(T)));

            if (File.Exists(filePath))
            {
                try
                {
                    string obj = File.ReadAllText(filePath);
                    tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize <T>(DataProtection.Unprotect(obj)));
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }
            else
            {
                tcs.SetResult(default(T));
            }
            return(tcs.Task);
        }
示例#2
0
        public Task StoreAsync <T>(string key, T value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key MUST have a value");
            }

            string serialized = NewtonsoftJsonSerializer.Instance.Serialize(value);
            string filePath   = Path.Combine(_folderPath, GenerateStoredKey(key, typeof(T)));

            File.WriteAllText(filePath, DataProtection.Protect(serialized));
            return(TaskEx.Delay(0));
        }