From 5f08dbafd1e1fd5d26deece2afde2d1f785bb8ef Mon Sep 17 00:00:00 2001 From: Jeroen Date: Tue, 9 Jun 2020 17:46:24 +0200 Subject: [PATCH] Force set cookie if exception occurs when getting cookie --- src/CookieManager/DefaultCookieManager.cs | 120 ++++++++++------------ 1 file changed, 56 insertions(+), 64 deletions(-) diff --git a/src/CookieManager/DefaultCookieManager.cs b/src/CookieManager/DefaultCookieManager.cs index 8e6ad06..cc83269 100644 --- a/src/CookieManager/DefaultCookieManager.cs +++ b/src/CookieManager/DefaultCookieManager.cs @@ -4,10 +4,10 @@ namespace CookieManager { - /// - /// Implementation of - /// - public class DefaultCookieManager : ICookieManager + /// + /// Implementation of + /// + public class DefaultCookieManager : ICookieManager { private readonly ICookie _cookie; @@ -22,30 +22,26 @@ public bool Contains(string key) return _cookie.Contains(key); } - /// - /// Get the value or set the value if specified key is expire - /// - /// TSource - /// Key - /// action to execute - /// Expire time - /// TSource object - public T GetOrSet(string key, Func acquirer, int? expireTime = default(int?)) + /// + /// Get the value or set the value if specified key is expire + /// + /// TSource + /// Key + /// action to execute + /// Expire time + /// TSource object + public T GetOrSet(string key, Func acquirer, int? expireTime = default) { - if(_cookie.Contains(key)) + try { - //get the existing value - return GetExisting(key); + return GetExisting(key); } - else - { - var value = acquirer(); - this.Set(key, value, expireTime); + catch { } - return value; - } + var value = acquirer(); + Set(key, value, expireTime); - return GetExisting(key); + return value; } private T GetExisting(string key) @@ -58,59 +54,55 @@ private T GetExisting(string key) return JsonConvert.DeserializeObject(value); } - /// - /// remove the key - /// - /// + /// + /// remove the key + /// + /// public void Remove(string key) { _cookie.Remove(key); } - /// - /// set the value - /// - /// key - /// value - /// + /// + /// set the value + /// + /// key + /// value + /// public void Set(string key, object value, int? expireTime = default(int?)) { _cookie.Set(key, JsonConvert.SerializeObject(value), expireTime); } - - /// - /// get the value of specified key - /// - /// T object - /// Key - /// T object - public T Get(string key) + + /// + /// get the value of specified key + /// + /// T object + /// Key + /// T object + public T Get(string key) { return GetExisting(key); } - public void Set(string key, object value, CookieOptions option) - { - _cookie.Set(key, JsonConvert.SerializeObject(value), option); - } - - public T GetOrSet(string key, Func acquirer, CookieOptions option) - { - if (_cookie.Contains(key)) - { - //get the existing value - return GetExisting(key); - } - else - { - var value = acquirer(); - this.Set(key, value, option); - - return value; - } - - return GetExisting(key); - } - } + public void Set(string key, object value, CookieOptions option) + { + _cookie.Set(key, JsonConvert.SerializeObject(value), option); + } + + public T GetOrSet(string key, Func acquirer, CookieOptions option) + { + try + { + return GetExisting(key); + } + catch { } + + var value = acquirer(); + Set(key, value, option); + + return value; + } + } }