More in this section

Forums / Developing with Sitefinity / Problems with Caching

Problems with Caching

12 posts, 0 answered
  1. Adam
    Adam avatar
    63 posts
    Registered:
    30 Jan 2008
    29 Jan 2009
    Link to this post
    I have created some custom controls to display data from an existing database.  The problem I'm running into is that updates to the existing database does not reset the cache for the page that has the custom control in it.  so the new data is not getting displayed until the cache times out on it's own.  

    I have tried turning the cache off using the *NO*CACHE* attribute as described in the User Manual but that hasn't helped.  It's still caching the external data.

    Is there a way from to manually reset the cache or is the different way to turn of the cache completely other than the *NO*CACHE* value? 
  2. Vlad
    Vlad avatar
    498 posts
    Registered:
    19 Jun 2017
    02 Feb 2009
    Link to this post

    Hello Adam,

    I am not sure that the problem is related to the Sitefinity caching. Setting the cachingProviderName of the cms section to *NO*CACHE* should disable the cache for the pages globally. Additionally, any change to the web.config should recycle the application pool and clear the cache. So, this action should reset the cache for sure.

    Could you please check if the whole page output is cached, not only the control output? You can do this if you add to the page a control, which displays the current time.

    Otherwise, Sitefinity caching supports the described from you scenario:
    You can cache a page, and add a control to it, which displays a data from the database. The cached page output is depending on the changes made to the data displayed in the control. In this case the controls should implement ICachableObject interface to provide CacheDependency for the page:

    public interface ICachableObject     
    {     
      CacheDependency[] GetDependencies();     
    }    
     

    Also the improved caching mechanism in the upcoming Sitefinity 3.6 will have two powerful features:
      - ability to specify caching for specific pages separately (you have an option to set cache duration and sliding expiration for each page);
      - cache substitution controls (your custom web or user controls can implement a base class, which provides an ability to render dynamic data within the cached output HTML).


    Greetings,

    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    17 Feb 2009
    Link to this post
    Hi Vlad ,

    I am using 3.6 and I am dealing with this issue.  Can you please point me to some kind of documentation or explain exactly how implement a no-caching for a specific control. "cache substitution controls"

    Thanks,

    Jeremy
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    17 Feb 2009
    Link to this post
    Hi Jeremy,

    We will update the documentation within the next few days. Generally, if you want to use substitution controls you have to inherit CacheSubstitutionUserControl class ( Telerik.Cms.Web.UI)
    You can also use CacheSubstitutionCompositeControl.

    Example:

    public partial class UserControls_FinalLoginControls_LoginControl : CacheSubstitutionUserControl  
    {  
        public virtual SubstitutionPageMode PageMode  
        {  
            get  
            {  
                return SubstitutionPageMode.Partial;  
            }  
        }  

    I hope this helps.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    17 Feb 2009
    Link to this post
    Not sure I understand how to implement this.  I tried this..but it doesnt seem to do the trick.  Can you explain how it should be done more clearly?  Here is my control. 

    using System; 
    using System.Collections.Generic; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Configuration; 
    using Telerik.Security; 
    using Telerik.Blogs; 
    using Telerik.Cms.Engine; 
    using System.Web.Security; 
    using Telerik.Cms.Web.UI; 
    public partial class _shared_Sitefinity_UserControls_Product_ProFav : CacheSubstitutionUserControl  
        public string site = ConfigurationSettings.AppSettings["Site"]; 
        public string favLinks; 
        CMethods cm = new CMethods(); 
        UserMethods um = new UserMethods(); 
        Guid UserID; 
        ProFavMethods pf = new ProFavMethods(); 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            string prodUrl=cm.stripAspx(Request.Path.ToString()); 
            MembershipUser currUser = um.CurrentUser(); 
            UserID = new Guid(currUser.ProviderUserKey.ToString()); 
            bool isMyFav = pf.IsMyFav(prodUrl, UserID); 
            if (isMyFav) favLinks = cm.GetTemplate("~/_shared/Sitefinity/UserControlTemplates/Product/proFav.ascx", "remove"); 
            else favLinks = cm.GetTemplate("~/_shared/Sitefinity/UserControlTemplates/Product/proFav.ascx", "add"); 
        } 
        public virtual SubstitutionPageMode PageMode 
        { 
            get 
            { 
                return SubstitutionPageMode.Partial; 
            } 
        } 
     


    and the .ascx just outputs the value of favLinks.


  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Feb 2009
    Link to this post
    Hello Jeremy,

    Try putting the code you have under OnPreRender() and use SubstitutionPageMode.Full;

    protected override void OnPreRender(EventArgs e) 
        { 
     
            base.OnPreRender(e); 
     
          // your code here 
        } 
     
     
        public override SubstitutionPageMode PageMode 
        { 
            get 
            {   
                // here use full mode 
                return SubstitutionPageMode.Full; 
            } 
        } 

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    18 Feb 2009
    Link to this post
    Hi. Thanks for the help.  I am trying what you are showing but it is only running my code 1 time, then it is cached on refresh...event if I clear browser cache.  Are there some other web.config settings that I need to implement to make this work?  Here is what I am trying now.

    public partial class _shared_Sitefinity_UserControls_Product_ProFav : CacheSubstitutionUserControl  
        public string site = ConfigurationSettings.AppSettings["Site"]; 
        public string favLinks; 
        CMethods cm = new CMethods(); 
        UserMethods um = new UserMethods(); 
        Guid UserID; 
        ProFavMethods pf = new ProFavMethods(); 
        protected void Page_Load(object sender, EventArgs e) 
        { 
             
        } 
        protected override void OnPreRender(EventArgs e) 
        { 
     
            base.OnPreRender(e); 
            string prodUrl = cm.stripAspx(Request.Path.ToString()); 
            MembershipUser currUser = um.CurrentUser(); 
            UserID = new Guid(currUser.ProviderUserKey.ToString()); 
            bool isMyFav = pf.IsMyFav(prodUrl, UserID); 
            if (isMyFav) 
            { 
                favLinks = "<a href='#' class='x_removeFav'><img alt='Remove My Favorite' src='/Images/_shared/btn_makefav_checked.png'/></a>"
            } 
            else 
            { 
                favLinks = "<a href='#' class='x_addFav'><img alt='Make My Favorite' src='/Images/_shared/btn_makefav_notchecked.png'/></a>"
            } 
        } 
     
     
        public override SubstitutionPageMode PageMode 
        { 
            get 
            { 
                // here use full mode  
                return SubstitutionPageMode.Full; 
            } 
        }  

  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Feb 2009
    Link to this post
    Hi Jeremy,

    Try using the ASPNET caching provider. You can set it in your web.config file.

    <caching defaultProvider="ASPNET"


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    18 Feb 2009
    Link to this post
    I actually tried this.. but it would still cache it.  My solution for now is to set cache to 5 seconds and remove sliding.  When I did try this method, it would either cache control along with the page, or sometimes, my page would stick the control at the top of the page and everything would break.
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    23 Feb 2009
    Link to this post
    Hello Jeremy,

    I tried using a simple control and it was working at my end. Here is the sample

    public partial class CustomControls_CacheControl : CacheSubstitutionUserControl 
        protected void Page_Load(object sender, EventArgs e) 
        { 
        } 
     
        protected override void OnPreRender(EventArgs e) 
        { 
     
            base.OnPreRender(e); 
     
            // here if the user is auth the image will be shown otherwise false 
            if (!Page.User.Identity.IsAuthenticated) 
            { 
                img.Visible = false
            } 
        } 
     
     
        [WebEditor("Telerik.FileManager.UrlWebEditor, Telerik.FileManager")] 
        public virtual string Image 
        { 
            get 
            { 
                return img.ImageUrl; 
            } 
            set 
            { 
                this.img.ImageUrl = value; 
            } 
        } 
        private string image; 
     
        public override SubstitutionPageMode PageMode 
        { 
            get 
            {   
                // Parital or Full 
                return SubstitutionPageMode.Parital; 
            } 
        } 
     


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  11. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    24 Oct 2009
    Link to this post
    Hi Ivan

    I've been playing with this a bit, we find we have controls with client side script, but the script is dynamic and registered on the server side

    ex:

    string initscript = "the time is " + DateTime.Now.ToLongTimeString();

     if (!this.Page.ClientScript.IsClientScriptBlockRegistered("contentRotatorScript"))
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "contentRotatorScript", initScript, true);

    **note, we don't actually do this for time, but it gets the point across.


    From what I've found, if we setup our control that does the script registration as shown above in Full substitution mode, the script is not updated on the client side.  Even if I take the "if" statement out.

    Is there a way to use this where we are registering script like this?
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    30 Oct 2009
    Link to this post
    Hello Adam @Habanero,

    Try not registering the script, but rendering it. You can do it by overriding Render method. The problem comes from the render, because the script registering is in a new stage that is not reached.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
12 posts, 0 answered