PHP Classes

How to Reduce the Costs of Hosting PHP Applications with the Compression Support of PHP memcached Extension - File cache class package blog

Recommend this page to a friend!
  All package blogs All package blogs   File cache class File cache class   Blog File cache class package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Reduce the Cos...  
  Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  

Author:

Updated on: 2023-05-23

Posted on: 2023-05-23

Viewers: 231 (May 2023)

Package: File cache class

One way to make PHP applications and sites faster is to use a caching method to store frequently used information, for instance, the results of database queries or the HTML contents of pages that do not change often.

In PHP, you can use a simple file-based cache system. However, a more efficient way to cache data is to use Memcached servers using the PHP Memcached extension.

This extension was improved by using compression to reduce the memory it takes to store cached data.

Read this article to learn more about making PHP applications faster by using cache solutions and how to use the latest version of the PHP Memcached solution to make PHP quicker and reduce the hosting costs of your PHP applications.




Loaded Article

In this article you may learn about:

1. What is the PHP memcached Extension

2. Why the PHP Memcached Extension Can Help Improving the Performance of PHP Applications

3. How to Benefit from the PHP Memcached Compression Support

4. How You Can Try the Newer Versions of the PHP Memcached Extension

1. What is the PHP memcached Extension

memcached is the name of a software tool that can store data in memory. Each piece of data stored by memcached is associated with a key.

memcached acts as a server that client applications can connect using a network connection.

Client applications can request that the memcached server performs operations like adding, removing, changing, and deleting data associated with given keys.

The PHP memcached is a PHP extension that acts as a client of Memcached servers and can request to manipulate data values of given keys.

2. Why the PHP Memcached Extension Can Help Improving the Performance of PHP Applications

PHP is already a very fast modern language. Usually, tasks that require access to large amounts of data can slow down PHP applications. That is the case when the applications need to access databases or services made available in external Web service servers.

One way to speed up PHP applications is to use caching solutions that store data in local cache systems retrieved from databases or external Web services. This way, access to frequently requested data is much faster.

For instance, let's say that you need to access a database to get the content of a blog post like this that you are reading now.

If many users access this page simultaneously, it will take a long time because the information we see on the post is obtained by executing many database queries.

After being published, it is unlikely that this article will be changed. Therefore, the PHP code on this site uses a caching system based on the File access class . This package can store in local files the contents of this page on the first time that this page is generated.

The next time this page is generated, this package will retrieve the contents of the page from a cache. This way, the site avoids executing the same database queries whenever this article is displayed to a new user.

/*
 * First create an object of the class.
 */
$cache_object=new file_cache_class;

/*
 * Then set the variable of the path of the cache file.
 * In the end, take a look at the contents of this file.
 */
$cache_object->path='mycachefile.cache';

/*
 * Now verify if the cache file exists is upto date.
 */
$success=$cache_object->verifycache($updated);

/*
 * Check the success value to make sure there was no error.
 */
if($success)
{

	/*
	 * Is the cache file upto date?
	 */
	if($updated)
	{

		/*
		 * Yes. Now, let's read from the cache file and output the stored data.
		 */
		$endofcache = false;
		for(;!$endofcache;)
		{
			$success = $cache_object->retrievefromcache($data,$endofcache);
			if(!($success))
				break;
			echo $data;
		}
	}
	else
	{

		/*
		 * No. The cache file needs to be regenerated from fresh data.
		 * Let's say that this is all the data I need to store in the cache.
		 */
		$data="Hello world! :-)\n";

		/*
		 * If necessary, set the cache timeout period in seconds.
		 */
		$cachetimeout = 60;
		$cache_object->setexpirytime($cachetimeout);

		/*
		 * Store the cache data, all at once.
		 */
		$success = $cache_object->storedata($data, true);

		/*
		 * If it was all ok, let's display the data.
		 */
		if($success)
		{
			echo $data;
		}
	}

    /*
     * Finally, let's just do some error handling.
     */
    if(!($success))
    {
        echo ('Error: '.$cache_object->error."\n");
    }
}

This is great, but it has a limitation. If a server on which this site is running gets overloaded by the accesses of many users, we may need to use more servers to handle the additional load.

When we use multiple Web servers, the file cache class mentioned above is no longer suitable because each server can only access cache files very quickly when the files are on the same server.

To overcome this limitation, we can use one or more memcached servers. A memcached server can run on the same machine running the Web server that serves pages generated by a PHP application.

Since memcached servers can also be on a different machine from the Web servers that serve Web pages, we can implement a more scalable solution to implement a cache system.

So instead of using the file cache class mentioned above, we can use a similar class that accesses a memcached server instead of local files.

The PHP memcached extension can be used to access to a memcached server.

3. How to Benefit from the PHP Memcached Compression Support

Recently, Rasmus Lerdorf, the creator of the PHP language, announced on Twitter that he was working on improving the PHP memcached extension that uses compression to store cached data.

Compression reduces the amount of memory used to store large amounts of data.

This way, you can store more cached data in the same server with the same RAM.

If you already hired a server machine, you may be aware that if you want to use more RAM in a server, you need to pay more money to the hosting company to install more RAM or to hire servers with more RAM.

Therefore this improvement in supporting compression that is now available in the latest version of the PHP memcached extension is welcome because you can save on the hosting expenses of server machines that you may use the memcached servers.

4. How You Can Try the Newer Versions of the PHP Memcached Extension

The development work of the PHP memcached extension is available in a git repository in GitHub .

If you want to use the latest version of the PHP memcached extension, you need to clone its Git repository, compile the wing, and install it in your PHP environment.

git clone https://github.com/php-memcached-dev/php-memcached.git

This process may be too complex if you do not have experience installing PHP extensions from source code. In that case, you may need to wait for the next PHP release, including the new version of the PHP Memcache-dev extension, so you can upgrade to the latest version and benefit from the new compression support feature.

Alternatively, if you just use one server machine, the file cache class can still be a good solution.

If you want to use this class you can either download its code or install it with the PHP Composer tool by following the instructions in the file cache class download page and click on the Install with Composer to see the lines that you need to add to your composer.json project file.

[Faster and cheaper PHP image] XXX

1. What is the PHP memcached Extension

memcached is the name of a software tool that can store data in memory. Each piece of data stored by memcached is associated with a key.

memcached acts as a server that client applications can connect using a network connection.

Client applications can request that the memcached server performs operations like adding, removing, changing, and deleting data associated with given keys.

The PHP memcached is a PHP extension that acts as a client of Memcached servers and can request to manipulate data values of given keys.

2. Why the PHP Memcached Extension Can Help Improving the Performance of PHP Applications

PHP is already a very fast modern language. Usually, tasks that require access to large amounts of data can slow down PHP applications. That is the case when the applications need to access databases or services made available in external Web service servers.

One way to speed up PHP applications is to use caching solutions that store data in local cache systems retrieved from databases or external Web services. This way, access to frequently requested data is much faster.

For instance, let's say that you need to access a database to get the content of a blog post like this that you are reading now.

If many users access this page simultaneously, it will take a long time because the information we see on the post is obtained by executing many database queries.

After being published, it is unlikely that this article will be changed. Therefore, the PHP code on this site uses a caching system based on the File access class . This package can store in local files the contents of this page on the first time that this page is generated.

The next time this page is generated, this package will retrieve the contents of the page from a cache. This way, the site avoids executing the same database queries whenever this article is displayed to a new user.

/*
 * First create an object of the class.
 */
$cache_object=new file_cache_class;

/*
 * Then set the variable of the path of the cache file.
 * In the end, take a look at the contents of this file.
 */
$cache_object->path='mycachefile.cache';

/*
 * Now verify if the cache file exists is upto date.
 */
$success=$cache_object->verifycache($updated);

/*
 * Check the success value to make sure there was no error.
 */
if($success)
{

	/*
	 * Is the cache file upto date?
	 */
	if($updated)
	{

		/*
		 * Yes. Now, let's read from the cache file and output the stored data.
		 */
		$endofcache = false;
		for(;!$endofcache;)
		{
			$success = $cache_object->retrievefromcache($data,$endofcache);
			if(!($success))
				break;
			echo $data;
		}
	}
	else
	{

		/*
		 * No. The cache file needs to be regenerated from fresh data.
		 * Let's say that this is all the data I need to store in the cache.
		 */
		$data="Hello world! :-)\n";

		/*
		 * If necessary, set the cache timeout period in seconds.
		 */
		$cachetimeout = 60;
		$cache_object->setexpirytime($cachetimeout);

		/*
		 * Store the cache data, all at once.
		 */
		$success = $cache_object->storedata($data, true);

		/*
		 * If it was all ok, let's display the data.
		 */
		if($success)
		{
			echo $data;
		}
	}

    /*
     * Finally, let's just do some error handling.
     */
    if(!($success))
    {
        echo ('Error: '.$cache_object->error."\n");
    }
}

This is great, but it has a limitation. If a server on which this site is running gets overloaded by the accesses of many users, we may need to use more servers to handle the additional load.

When we use multiple Web servers, the file cache class mentioned above is no longer suitable because each server can only access cache files very quickly when the files are on the same server.

To overcome this limitation, we can use one or more memcached servers. A memcached server can run on the same machine running the Web server that serves pages generated by a PHP application.

Since memcached servers can also be on a different machine from the Web servers that serve Web pages, we can implement a more scalable solution to implement a cache system.

So instead of using the file cache class mentioned above, we can use a similar class that accesses a memcached server instead of local files.

The PHP memcached extension can be used to access to a memcached server.

3. How to Benefit from the PHP Memcached Compression Support

Recently, Rasmus Lerdorf, the creator of the PHP language, announced on Twitter that he was working on improving the PHP memcached extension that uses compression to store cached data.

Compression reduces the amount of memory used to store large amounts of data.

This way, you can store more cached data in the same server with the same RAM.

If you already hired a server machine, you may be aware that if you want to use more RAM in a server, you need to pay more money to the hosting company to install more RAM or to hire servers with more RAM.

Therefore this improvement in supporting compression that is now available in the latest version of the PHP memcached extension is welcome because you can save on the hosting expenses of server machines that you may use the memcached servers.

4. How You Can Try the Newer Versions of the PHP Memcached Extension

The development work of the PHP memcached extension is available in a git repository in GitHub .

If you want to use the latest version of the PHP memcached extension, you need to clone its Git repository, compile the wing, and install it in your PHP environment.

git clone https://github.com/php-memcached-dev/php-memcached.git

This process may be too complex if you do not have experience installing PHP extensions from source code. In that case, you may need to wait for the next PHP release, including the new version of the PHP Memcache-dev extension, so you can upgrade to the latest version and benefit from the new compression support feature.

Alternatively, if you just use one server machine, the file cache class can still be a good solution.

If you want to use this class you can either download its code or install it with the PHP Composer tool by following the instructions in the file cache class download page and click on the Install with Composer to see the lines that you need to add to your composer.json project file.




You need to be a registered user or login to post a comment

1,611,062 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

1. Any chance of similar feature in Redis. - Ramesh Narayan Jangid (2023-05-23 13:53)
Memcache and Redis are caching tools which works on similar line... - 1 reply
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  
  All package blogs All package blogs   File cache class File cache class   Blog File cache class package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Reduce the Cos...