caching | Dmytro Shteflyuk's Home https://kpumuk.info In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming. Mon, 07 Sep 2015 23:49:09 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Correct using Cache in ASP.NET https://kpumuk.info/asp-net/correct-using-cache-in-aspnet/ https://kpumuk.info/asp-net/correct-using-cache-in-aspnet/#comments Sat, 12 Jan 2008 19:22:00 +0000 http://kpumuk.info/asp-net/correct-using-cache-in-aspnet/ Often in ASP.NET application we see a code which looks like this one: 12345if (Cache["SomeData"] != null) {     string name = ((SomeClass) Cache["SomeData"]).Name;     //..... } Experienced developer, even if he is not a paranoiac, will find possible problem immediately — NullReferenceException. That’s because of caching implementation in ASP.NET. In ideal case […]

The post Correct using Cache in ASP.NET first appeared on Dmytro Shteflyuk's Home.]]>
Often in ASP.NET application we see a code which looks like this one:

1
2
3
4
5
if (Cache["SomeData"] != null)
{
    string name = ((SomeClass) Cache["SomeData"]).Name;
    //.....
}

Experienced developer, even if he is not a paranoiac, will find possible problem immediately — NullReferenceException. That’s because of caching implementation in ASP.NET. In ideal case an object, that has been cached, will stay there up to application restart, but in real world it could be deleted between two calls: by the garbage collector when memory is over (because cache uses weak references WeakReference); by another thread to refresh cached data.

So the code I have mentioned before works in 99% of all cases, but sometimes you will get errors in your log, which can not be reproduced easily. Here is right cache usage approach:

1
2
3
4
5
6
SomeClass someClass = Cache["SomeData"] as SomeClass;
if (someClass != null)
{
    string name = someClass.Name;
    //.....
}

Do not relax your vigilance, it’s exactly what they are waiting for! (about bugs)

The post Correct using Cache in ASP.NET first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/asp-net/correct-using-cache-in-aspnet/feed/ 5
Correct Last-Modified header processing https://kpumuk.info/php/correct-last-modified-header-processing/ https://kpumuk.info/php/correct-last-modified-header-processing/#comments Mon, 10 Apr 2006 20:54:25 +0000 http://kpumuk.info/php/correct-last-modified-header-processing/ This is quick post about Last-Modified header. Please imagine following situation: you have image stored in your database and you need to send it to the browser on some request. But image extraction from database takes some time, and if there are more than one image you Web-server’s productivity will decrease dramatically. Is this case […]

The post Correct Last-Modified header processing first appeared on Dmytro Shteflyuk's Home.]]>
This is quick post about Last-Modified header. Please imagine following situation: you have image stored in your database and you need to send it to the browser on some request. But image extraction from database takes some time, and if there are more than one image you Web-server’s productivity will decrease dramatically. Is this case you need to implement caching functionality in your application. All images can be changed therefor you need to have ability to check image modified date (for example, this date can be stored in same database).

Every browser has its own cache, and if you will send right Cache-Control header, your image will be cached for some time. After cache time expiring browser will send request to your application to get fresh version of image. But if your image was not updated you can send 304 code in response to tell browser about this. Look at the following code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// --- Your code ---

// You need past following before any output

// here you need to select modified date from DB in UNIX timestamp format
// (same as time() function in PHP or UNIX_TIMESTAMP() function in MySQL)
$date = time();

$last_modified = gmdate('D, d M Y H:i:s', $date) . ' GMT';
// did the browser send an if-modified-since request?
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  // parse header
  $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);

  if ($if_modified_since == $last_modified) {
    // the browser's cache is still up to date
    header('HTTP/1.0 304 Not Modified');
    header('Cache-Control: max-age=86400, must-revalidate');
    exit;
  }
}

header('Cache-Control: max-age=86400, must-revalidate');
header('Last-Modified: ' . $last_modified);

// --- Your code ---

In this code I check HTTP_IF_MODIFIED_SINCE request variable which will be sent by browser when cache is expired. If image was not modified, special header will be sent. Default cache period in this sample is 24 hours (86400 seconds). This is very simple and powerful issue which can be used in your Web-application as-is to increase perfomance.

Hint: you can create static-like URLs using following lines in .htaccess file (mod_rewrite):

1
2
RewriteEngine On
RewriteRule ^image_([0-9]+).php$ getimage.php?image=$1 [L]

In this case your images will have URLs like http://example.com/image_10.php, and all request will be redirected to getimage.php with parameter image=10.

Useful links

The post Correct Last-Modified header processing first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/php/correct-last-modified-header-processing/feed/ 12
Why does IE save pictures from the Web in bitmap format? https://kpumuk.info/php/ie-save-pictures-bmp/ https://kpumuk.info/php/ie-save-pictures-bmp/#comments Wed, 18 Jan 2006 12:46:17 +0000 http://kpumuk.info/php/ie-save-pictures-bmp/ Today I found very strange problem with Internet Explorer. I’ve generated GIF-image with PHP GD2 and sent results for client browser. In IE I’ve tried to store picture in my local file system but the only option was to save the picture as a .BMP file instead of as a native .gif file! The same […]

The post Why does IE save pictures from the Web in bitmap format? first appeared on Dmytro Shteflyuk's Home.]]>
Today I found very strange problem with Internet Explorer. I’ve generated GIF-image with PHP GD2 and sent results for client browser. In IE I’ve tried to store picture in my local file system but the only option was to save the picture as a .BMP file instead of as a native .gif file! The same problem is described in Microsoft Knowledge Base, but it does not help me.

Then I start playing with PHP and headers and discover very nice gizmo: bug became apparent only when my PHP-script uses session_start(). But I needed sessions! Solution was very simple: it was necessary to add following lines before image output:

1
2
3
header("Cache-Control:");
header("Pragma:");
header("Set-Cookie:");

These lines will clear all headers sent by session_start() function and IE will understand that your the image is in right format.

The post Why does IE save pictures from the Web in bitmap format? first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/php/ie-save-pictures-bmp/feed/ 7