h***@gmail.com
2016-06-07 21:56:43 UTC
We have a security wrapper that goes around all our MVC projects. We start first with the security wrapper because it has all the stuff concerning Roles that should have been made simpler by MS by now (this is just my opinion, I'm sure no one else thinks this. . .).
Now the person before me that build this wrapper included a call to Singleton to clear the cache if it was causing problems. It comes off the shelf without any cache items configured.
But I am almost sure this Singleton could be replaced with something simpler for just clearing the cache by the admin pushing a button. I've inherited the responsibility of maintaining and improving this security wrapper. Surely there is something simpler.
====================================
In the View there is an Action Link
====================================
@Html.ActionLink("Refresh Cache", "Refresh", "CacheManagement", new { @class = "btn btn-default" })
============================
that calls the ActionResult
============================
[HttpGet]
public ActionResult Refresh()
{
Singleton.GetInstance().RefreshCache();
ViewBag.Text = "Refreshed";
return View("Index");
}
========================
That runs the Singleton
========================
namespace security6wrapper.Utilities
{
public sealed class Singleton
{
private static Singleton _instance;
private string _value;
private readonly object _lock = new object();
//Constructor
private Singleton()
{
_value = string.Empty;
}
/// <summary>
/// Get an instance
/// </summary>
/// <returns></returns>
public static Singleton GetInstance()
{
return _instance ?? (_instance = new Singleton());
}
/// <summary>
/// Get Value
/// </summary>
/// <returns></returns>
public string GetValue()
{
return _value;
}
/// <summary>
/// Clears the cache;
/// </summary>
//[Authorize(Roles = Security.AdminRole)]
public void RefreshCache()
{
lock (_lock)
{
//reset all stored objects
_value = string.Empty;
}
}
}
}
Now the person before me that build this wrapper included a call to Singleton to clear the cache if it was causing problems. It comes off the shelf without any cache items configured.
But I am almost sure this Singleton could be replaced with something simpler for just clearing the cache by the admin pushing a button. I've inherited the responsibility of maintaining and improving this security wrapper. Surely there is something simpler.
====================================
In the View there is an Action Link
====================================
@Html.ActionLink("Refresh Cache", "Refresh", "CacheManagement", new { @class = "btn btn-default" })
============================
that calls the ActionResult
============================
[HttpGet]
public ActionResult Refresh()
{
Singleton.GetInstance().RefreshCache();
ViewBag.Text = "Refreshed";
return View("Index");
}
========================
That runs the Singleton
========================
namespace security6wrapper.Utilities
{
public sealed class Singleton
{
private static Singleton _instance;
private string _value;
private readonly object _lock = new object();
//Constructor
private Singleton()
{
_value = string.Empty;
}
/// <summary>
/// Get an instance
/// </summary>
/// <returns></returns>
public static Singleton GetInstance()
{
return _instance ?? (_instance = new Singleton());
}
/// <summary>
/// Get Value
/// </summary>
/// <returns></returns>
public string GetValue()
{
return _value;
}
/// <summary>
/// Clears the cache;
/// </summary>
//[Authorize(Roles = Security.AdminRole)]
public void RefreshCache()
{
lock (_lock)
{
//reset all stored objects
_value = string.Empty;
}
}
}
}