Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[4318] .NET Data Caching

Last post 05-15-2008 3:11 PM by ojemuyiwa. 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4318] .NET Data Caching

    This thread is for discussions of .NET Data Caching.

    • Post Points: 0
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 02-19-2004 9:53 AM In reply to

    No success in Data Caching

    I created a class to test the data cache. Any help where to use cache?

    // test.cs
    //

    namespace xtest {
       using System;
       using System.Web;
       using System.Web.Caching;
       public class xtest {

           public static Cache srvCache = new Cache();

           public xtest() {}
           public static string getCache(string key){

               try {
               object strServer= 0;

                   if (srvCache["ServerString"] == null)
                     srvCache["ServerString"]=key;

                   strServer = srvCache.Get("ServerString");

                   return (string) strServer;
               }
               catch (Exception e)
               {
                   return "error: getCache() - "+ e.Message;
               }

           }
       }
    }

    When I run the below mentioned aspx. I get error:
                                   - Object reference not set to an instance of an object.


    <%@ Page Language="C#" %>
    <%@ import Namespace="xtest" %>
    <script runat="server">

       void Page_Load(object sender, EventArgs e) {


                Response.Write(xtest.getCache("X-STAGE"));

       }

    </script>
    <html>
    <head>
    </head>
    <body>
       <form runat="server">
           <!-- Insert content here -->
       </form>
    </body>
    </html>


    • Post Points: 0
  • 03-01-2004 9:55 AM In reply to

    • James Crowley
    • Top 10 Contributor
    • Joined on 12-07-2000
    • United Kingdom
    • Guru
    • Points 14,865
    • SystemAdministrator
    • Post Points: 0
  • 04-07-2004 3:17 PM In reply to

    How to synchronize caches across application insta

    How would one use the .NET cache object to keep caches accurate across processes?

    --Charlie


    • Post Points: 0
  • 07-16-2004 2:33 AM In reply to

    • kga3
    • Not Ranked
    • Joined on 07-16-2004
    • New Member
    • Points 5
    Yes I have tried that as well. IMHO: The thing is that you are trying to access a cache object from a class. But the documentation says that the Cache object cannot be inherited. therefore I would expect it to throw a "Cache is not available" exception. Now this is a real bugger specially when you want to cache data across process/pages. This is precisely what I'm trying to do right now and I have yet to find a solution. Help anyone?
    • Post Points: 0
  • 03-23-2005 2:08 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45

    Access a cache object from a class

    Hi all, this article unfortunately doesn't deal with class caching. However, in my latest article posted here on DeveloperFusion -Building a Full-Featured Custom DataGrid Control -  I do just that. I demonstrate how to cache an object, in this case the Datagrid, within a class using either the Session or Web Cache API.

    As for caching across processes, you'll need to use .NET remoting. The process is discussed here The process is discussed here Using Remoting Singleton Caching.

    Hope this helps.

    -DM
    • Post Points: 0
  • 03-23-2005 2:15 PM In reply to

    • DMarko1
    • Top 500 Contributor
    • Joined on 09-19-2003
    • Addicted Member
    • Points 45

    Hi Charlie,

    You can use .NET remoting when you need to keep your cache across processes. The process is discussed here Using Remoting Singleton Caching.

    Hope this helps.

    -DM
    • Post Points: 0
  • 04-30-2005 5:00 AM In reply to

    • muthup
    • Not Ranked
    • Joined on 04-30-2005
    • New Member
    • Points 10

    Cache is not Available

    I stumbled into this problem. The comment is correct The Cache class is a sealed class, which means it cannot be inherited. Since you are inheriting the Page class in your X testClass the Cache object will  not be available. One way to solve the problem is to pass the Cache object of the Page to the Method that need to process the Cache information as shown in the Sample code..


    <%@ import Namespace="Env" %>
    <%@ import Namespace="System.Xml" %>
    <%@ Page Language="C#" Inherits ="Env.ServerProcess" %>
    <%
       
    XmlDocument xDoc = new  XmlDocument();
    xDoc=ProcessRequest(Request.InputStream,this.Cache);
    Response.ContentType="text/xml";
    Response.Write (xDoc.OuterXml);
    %>
    public class ServerProcess : System.Web.UI.Page
    {
           
       public  XmlDocument ProcessRequest(Stream PageRequestStream,Cache MyCache)
           {
               string sResetClientId="";
              XmlDocument xdoc =new XmlDocument();
                try{
                  if (MyCache ["ResetClientDoc"] == null){
           TextReader xt=new StreamReader(Server.MapPath(@"Content/ResetClientID.xml"));
           sResetClientId=xt.ReadToEnd();
           xt.Close();
           MyCache dep = new CacheDependency(Server.MapPath(@"Content/ResetClientID.xml"), DateTime.Now);
                   
           MyCache.Insert("ResetClientDoc", sResetClientId, dep);}
           Else
    {
              sResetClientId=(string) MyCache ["ResetClientDoc"];

           }
                                 }
                          catch (Exception e){
           System.Diagnostics.Debug.WriteLine( e.Message.ToString())
       }
    xdoc.LoadXml(sResetClientId);
    return xdoc;
       
    }
    • Post Points: 0
  • 04-30-2005 10:01 AM In reply to

    • James Crowley
    • Top 10 Contributor
    • Joined on 12-07-2000
    • United Kingdom
    • Guru
    • Points 14,865
    • SystemAdministrator
    Just because the Cache cannot be inherited, doesn't mean you can't access it when you are inheriting the Page class -

    public class MyClass : System.Web.UI.Page {
      public Object SomeMethod() {
         return Cache["SomeObject"];
      }
    }

    is fine. As it's sealed, what you can't do is this:

    public class MyClass : Cache {
      // override or add some methods here
    }
    • Post Points: 0
  • 04-30-2005 11:05 PM In reply to

    • muthup
    • Not Ranked
    • Joined on 04-30-2005
    • New Member
    • Points 10

    Cache is not Available

    So , Does That mean  this is a Genuine Microsoft Bug??
    No matter What If I Use this Syntax[ Colorede red] to acess the Cache Object I get an Exception...
    internal class EnvUtil ystem.Web.UI.Page {
               
    internal EnvUtil()
    {

    }
    internal  string ResetClientID
    {
    get
    {
    string sResetClientId="";
    try
    {
      if (Cache["ResetClientDoc"] == null)     {
               TextReader xt=new StreamReader(Server.MapPath(@"../../Content/EnvReport/ResetClientID.xml"));

               sResetClientId=xt.ReadToEnd();
               xt.Close();
               CacheDependency dep = new CacheDependency(Server.MapPath(@"../../Content/EnvReport/ResetClientID.xml"), DateTime.Now);

               Cache.Insert("ResetClientDoc", sResetClientId, dep);
       }
       else
       {
               sResetClientId=(string) Cache["ResetClientDoc"];
       }

    }
    catch (Exception ex)
    {
       Debug.WriteLine(ex.Message);
    }
       return sResetClientId;
    }
    }

    }
    }

    ======================
    Output
    ?ex.Message
    "Cache is not available"
    =======================
    • Post Points: 10
  • 05-15-2008 3:11 PM In reply to

    • ojemuyiwa
    • Not Ranked
    • Joined on 10-31-2007
    • United Kingdom
    • New Member
    • Points 5

    Re: Cache is not Available

    To Access the Pages Cache from class code c#

     

    Cache ce = new Cache();

     

    ce = System.Web.HttpRuntime.Cache; Lol

    • Post Points: 5
Page 1 of 1 (11 items)