473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Cache Absolute Expiry problem

Hi all,

I have one question. I want to put a class which has one dictionary property in the cache. And I want this cached data should expire after 5 minutes by using absolute expiry.

But I have one issue suppose one user is reading the Dictionary data from the cache and say 5 minutes over and the cached data is removed then while reading the data there may get object null reference error. Can anybody suggest me in this scenario how to proceed.
Oct 1 '10 #1
1 1140
balabaster
797 Expert 512MB
When the item is removed from the cache, it doesn't destroy the object. The cache should just hold a reference to the object.

Check this simplified example:

Expand|Select|Wrap|Line Numbers
  1. Hashtable cache = new Hashtable();
  2. MyClass stuff = new MyClass 
  3.     Property1 = "Hello", 
  4.     Property2 = "World"
  5. }
  6. cache.Add("stuff", stuff);
When you added the item to the hashtable, it doesn't actually add the object, but a reference to the object. The memory address if you will [that's simplified, don't read this too literally]. So the object is held in memory somewhere and a reference is held in the cache. The reference in the cache stops garbage collection from throwing it out until the reference is removed, thus it will sit in memory until there's no references left. When someone needs to make use of it, the reference is retrieved from the cache in order to locate the object in memory and now there's 2 references to the object. The cache is invalidated and the reference is removed, leaving 1 reference - the reference that's in use. The object stops being used and the reference goes out of scope leaving 0 references. At this point garbage collection reclaims the memory space held by the object.

If you're getting a null reference exception, then it's because you're making reference to your object in an invalid manner.

This occurs when:

Expand|Select|Wrap|Line Numbers
  1. string prop1 = cache["stuff"].Property1;
  2. //Cache is invalidated here...
  3. string prop2 = cache["stuff"].Property2; //Throws null reference exception
This can be avoided by doing this instead:
Expand|Select|Wrap|Line Numbers
  1. MyClass stuff = (MyClass)cache["stuff"];
  2. string prop1 = stuff.Property1;
  3. string prop2 = stuff.Property2;
Oct 1 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Matt | last post by:
Hello I am tring to figure out why our SQL server is a bit sluggish from time to time. It is running a dual XEON, with 2.5 GB RAM, and a fast SCSI I/O sub system setup as follows. OS,...
1
by: William Starr Moake | last post by:
Another problem with absolute paths in the WYSIWYG editor I'm putting together. The function to toggle between WYSIWYG and HTML modes works except for one glitch. If you use a relative path for...
0
by: Anand | last post by:
Change the mode="stateServer" and Datasource = localhost.. instead of 127.0.0.1 Setting goes like this <SessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424"...
2
by: Gareth | last post by:
Does anyone know how to make an ASP.NET (C#) page expire immediately and not be cached? What I want to happen is if the users pressed the BACK button they get a page has expired and has to be...
0
by: Walter Psaila | last post by:
Hi, We have an ASP.NET 2.0 web page with user authentication. We have a page which contains a Multiview with a number of different views. Each view represents one step in a whole process. The...
9
by: Pumkin | last post by:
Hello guys, I have a small problem with setting the absolute path of an image. Here is the scenario: I need to put the image in a text concat something like this: <asp:Label id = "lbl"...
0
by: PBsoft | last post by:
I installed Access Developer Extensions (VSTO 2005) but got a problem in caching installation languages for idioms different from italian or english. From the "Installer Experience" page of the...
0
by: Hypnotik | last post by:
My program is to simulate cache memory. I read in the info from 2 external files, 1) access 2) data in memory. When I read the information in I display the info...and it is all correct. However...
1
by: jamesm6162 | last post by:
I have an image contained inside a div element somewhere in my page. Using javascript in the onclick event of the image, I change the position of the image to absolute and change the top and left...
1
by: Maanav Jackson | last post by:
Hi I am displaying a third party webpage into a variable using curl library. When i am displaying the variable in the browser using echo command as shown below. echo $pagecopy; Browser is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.