using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.Caching; using System.Web.UI; using System.Web.UI.WebControls; using CMS.Controls; using CMS.GlobalHelper; using CMS.UIControls; using CMS.SettingsProvider; public partial class CMSModules_System_Debug_CacheItemsGrid : CMSUserControl, IUniPageable, IPostBackEventHandler { #region "Variables" protected int mTotalItems = 0; protected bool mShowDummyItems = false; protected IEnumerable mAllItems = null; #endregion #region "Properties" /// /// Event fired when some cache item is deleted. /// public event EventHandler OnItemDeleted; /// /// All cache items array. /// public IEnumerable AllItems { get { return mAllItems; } set { mAllItems = value; } } /// /// If true, grid shows the dummy items. /// public bool ShowDummyItems { get { return mShowDummyItems; } set { mShowDummyItems = value; } } /// /// Returns the pager control. /// public UniGridPager PagerControl { get { return pagerItems; } } /// /// Returns total number of items. /// public int TotalItems { get { return mTotalItems; } } #endregion protected override void OnInit(EventArgs e) { base.OnInit(e); pagerItems.PagedControl = this; //this.pagerItems.UniPager.PageControl = "cacheItemsGrid"; } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // Register delete script string script = "function DeleteCacheItem(key) { if (confirm(" + ScriptHelper.GetString(GetString("general.confirmdelete")) + ")) { document.getElementById('" + hdnKey.ClientID + "').value = key; " + Page.ClientScript.GetPostBackEventReference(this, "delete") + " } }\n" + "function Refresh() { " + Page.ClientScript.GetPostBackEventReference(this, "refresh") + " }\n" + "function Show(key) { var url = '" + ResolveUrl("System_ViewObject.aspx?source=cache&key=") + "' + key; window.open(url); }"; ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "DeleteCacheItem", ScriptHelper.GetScript(script)); } protected void btnSearch_Click(object sender, EventArgs e) { pagerItems.UniPager.CurrentPage = 1; } public void ReloadData() { if (ShowDummyItems) { lblKey.Text = GetString("Administration-System.CacheInfoDummyKey"); plcData.Visible = false; } else { lblKey.Text = GetString("Administration-System.CacheInfoKey"); lblData.Text = GetString("Administration-System.CacheInfoData"); lblExpiration.Text = GetString("Administration-System.CacheInfoExpiration"); lblPriority.Text = GetString("Administration-System.CacheInfoPriority"); plcData.Visible = true; plcContainer.Visible = CacheHelper.DebugCache; } lblAction.Text = GetString("General.Action"); // Build the table StringBuilder sb = new StringBuilder(); // Prepare the indexes for paging int pageSize = pagerItems.CurrentPageSize; int startIndex = (pagerItems.CurrentPage - 1) * pageSize + 1; int endIndex = startIndex + pageSize; // Process all items int i = 0; bool all = (endIndex <= startIndex); string search = txtFilter.Text; if (mAllItems != null) { if (ShowDummyItems) { // Process dummy keys foreach (string key in mAllItems) { if (key.IndexOfCSafe(search, true) >= 0) { if (!String.IsNullOrEmpty(key)) { // Process the key object value = HttpRuntime.Cache[key]; CacheItemContainer container = null; // Handle the container if (value is CacheItemContainer) { container = (CacheItemContainer)value; value = container.Data; } if (value == CacheHelper.DUMMY_KEY) { i++; if (all || (i >= startIndex) && (i < endIndex)) { RenderItem(sb, key, container, value, true, i); } } } } } } else { // Process only normal keys foreach (string key in mAllItems) { if (key.IndexOfCSafe(search, true) >= 0) { // Process the key object value = HttpRuntime.Cache[key]; CacheItemContainer container = null; // Handle the container if (value is CacheItemContainer) { container = (CacheItemContainer)value; value = container.Data; } if (value != CacheHelper.DUMMY_KEY) { i++; if (all || (i >= startIndex) && (i < endIndex)) { RenderItem(sb, key, container, value, false, i); } } } } } } mTotalItems = i; plcItems.Visible = (i > 0); lblInfo.Visible = (i <= 0); if (!RequestHelper.IsPostBack()) { pnlSearch.Visible = (i > 10); } ltlCacheInfo.Text = sb.ToString(); // Call page binding event if (OnPageBinding != null) { OnPageBinding(this, null); } } /// /// Renders the particular cache item. /// protected void RenderItem(StringBuilder sb, string key, CacheItemContainer container, object value, bool dummy, int i) { // Add the key row string cssClass = ((i % 2) == 0) ? "OddRow" : "EvenRow"; sb.Append(""); // Get the action GetDeleteAction(key, sb); sb.Append(""); string keyTag = TextHelper.LimitLength(key, 100); sb.Append(keyTag.Replace("&", "&")); sb.Append(""); if (!dummy) { sb.Append(""); // Render the value if (value != null) { sb.Append("\"");"); sb.Append(" "); if ((value == null) || (value == DBNull.Value)) { sb.Append("null"); } else { sb.Append(HttpUtility.HtmlEncode(DataHelper.GetObjectString(value, 100))); } } else { sb.Append("null"); } sb.Append(""); if (CacheHelper.DebugCache) { // Expiration sb.Append(""); if (container != null) { if (container.AbsoluteExpiration != Cache.NoAbsoluteExpiration) { sb.Append(container.AbsoluteExpiration); } else { sb.Append(container.SlidingExpiration); } } sb.Append(""); // Expiration sb.Append(""); if (container != null) { sb.Append(container.Priority); } sb.Append(""); } } sb.Append(""); } /// /// Gets the action. /// /// Cache key /// StringBuilder with the output protected void GetDeleteAction(string key, StringBuilder sb) { sb.Append(""); } #region "IUniPageable Members" /// /// Pager data item object. /// public object PagerDataItem { get { return null; } set { } } /// /// Pager control. /// public UniPager UniPagerControl { get; set; } /// /// Occurs when the control bind data. /// public event EventHandler OnPageBinding; /// /// Occurs when the pager change the page and current mode is postback => reload data /// public event EventHandler OnPageChanged; /// /// Evokes control databind. /// public void ReBind() { if (OnPageChanged != null) { OnPageChanged(this, null); } } /// /// Gets or sets the number of result. Enables proceed "fake" datasets, where number /// of results in the dataset is not correspondent to the real number of results /// This property must be equal -1 if should be disabled /// public int PagerForceNumberOfResults { get { return mTotalItems; } set { } } #endregion #region "IPostBackEventHandler Members" /// /// Processes the postback. /// /// Event argument public void RaisePostBackEvent(string eventArgument) { // Delete the item string key = hdnKey.Value; if (!String.IsNullOrEmpty(key)) { CacheHelper.Remove(key); // Raise the OnItemDeleted event if (OnItemDeleted != null) { OnItemDeleted(this, null); } } } #endregion }