Save PHP session to memory

Source: Internet
Author: User
Tags eval php session php script phpinfo sessions zend

Today has been studying how to save the session to memory, we first say why to save the session in memory, what is the benefit.
Because if Session_Start () is set up in our PHP program, then each person who accesses the file will produce a session file that exists in the system cache in the/tmp directory, and we often write to the shopping cart or member system to use the session, So if a large traffic site or session save time to set a very long site, will create a large number of session files in the/tmp directory, although sometimes the file size is only 0, but it does occupy a file handle, I have a server because the setting of the save time is too long , resulting in 120多万个 session, occupy the hard drive all the file handle, resulting in no longer create a new file, I spent 35 hours to remove this 120多万个 session, as long as you set the Session_Start (), The server will find the value of the session based on the user's local cookies, and if there is a saved session file, then retrieve the value of the session, if not so new to create a session file. Imagine, in the 120多万个 file to find the file you want, is how terrible things ah, I will command to wait a few minutes without response, so too much of the session file is bound to cause the system to run slowly, so I want to save the session to memory, This can greatly speed up the session of reading and writing speed, but also can reduce the loss of hard disk, both worlds. Ha ha
I found a lot of articles said that using a session of MM with shared memory module can do this work, just ports inside also have, I installed a bit, but also according to the instructions recompile PHP, and the php.ini Session.save_handler = Files changed to Session.save_handler = mm, the result I no matter how to call or a mistake said find Save_handler mm, this can worry me, did a morning, also did not find out the problem. Because the MM test did not succeed, I do not provide the installation of the steps of the MM.
As a result I was looking at phpinfo, occasionally in the session that section found registered save handlers = Files User Eaccelerator, oh, I installed the eaccelerator, Maybe it and mm can not coexist it. I put Session.save_handler = Eaccelerator, after the Apache, the normal operation. Go to/tmp below to see, really not in the generation of a new session file, haha.
Also need to remind everyone that, although the session saved to memory in a variety of benefits, but if the use of Apachectl restart, then the session in memory will be lost oh, so do not always back up Apache. Oh.
  

Don't know Eaccelerator and won't install Eaccelerator

What is eaccelerator.

Eaccelerator is an open source and free PHP accelerator, optimizer, encoder, and also the ability to provide dynamic content caching for PHP. It can cache PHP scripts as compiled to improve the performance of PHP scripts, so traditional precompilation is almost eliminated. Eaccelerator can also optimize PHP scripts to improve the execution speed of PHP scripts. Eaccelerator can effectively reduce server load and increase PHP program speed by up to 1-10 times.

Turckmmcache is the predecessor of Eaccelerator.
(Http://sourceforge.net/project/turckmm-cache/by Dmitry stogov)

Eaccelerator contains a PHP encoder and loader. You can use the encoder to encode the. php script so that you can publish your PHP program in a non source code way. The coded PHP program can be run on any site that has a PHP parsing environment and Eaccelerator installed, because the encoded PHP program is stored as compiled code, and the compiled version does not contain the source code of the program, so the PHP program after the Eaccelerator code is not That can be restored. Of course, some internal scripts can be restored by some different decompile engine tools (such as disassemblers, debuggers, etc.), but this is not trivial.

Eaccelerator is compatible with the Zend Optimizer loader. In php.ini, Zend Optimizer must be loaded after eaccelerator. If your site does not run any PHP scripts encoded via the Zend encoder, then we do not recommend that you install Zend Optimizer on the server where Eaccelerator is installed.

Eaccelerator cannot run in CGI mode, but it can run in fast-cgi mode similar to LIGHTTPD.

Here are some products that have the same functionality as Eaccelerator:
-Zend Performance Suite (http://www.zend.com)
-Alternative PHP Cache (Http://pecl.php.net/package/APC)

Eaccelerator has been a very common PHP Platform precompiled acceleration of the means, the installation of PHP after a significant increase in speed, so today we learn to install from the ports Eaccelerator, and configure it. Reference # Cd/usr/ports/www/eaccelerator
Make
Make install clean
Mkdir/tmp/eaccelerator
Chown-r Www/tmp/eaccelerator
chmod 0700/tmp/eaccelerator
Vi/usr/local/etc/php.ini



as Zend Extended installation profile
References zend_extension= "/usr/local/lib/php/20060613/eaccelerator.so"
[Eaccelerator]
eaccelerator.shm_size=
eaccelerator.cache_dir= "/tmp/eaccelerator"
eaccelerator.enable= "1"
Eaccelerator.optimizer= "1"
Eaccelerator.check_mtime= "1"
eaccelerator.debug= "0"
eaccelerator.filter= ""
eaccelerator.shm_max= "0"
eaccelerator.shm_ttl= "0"
eaccelerator.shm_prune_period= "0"
eaccelerator.shm_only= "0"
eaccelerator.compress= "1"
eaccelerator.compress_level= "9"



Install configuration file as standalone module
Reference [eaccelerator]
extension= "eaccelerator.so"
eaccelerator.shm_size= 0″
Eaccelerator.cache_dir= "/tmp/eaccelerator"
eaccelerator.enable= "1″
eaccelerator.optimizer=" 1″
Eaccelerator.check_mtime= "1″
eaccelerator.debug=" 0″
eaccelerator.filter= "
eaccelerator.shm_max=" 0″
eaccelerator.shm_ttl= "0″
eaccelerator.shm_prune_period=" 0″
eaccelerator.shm_only= "0″
eaccelerator.compress= "1″
eaccelerator.compress_level=" 9″



This is the arthurxf,session all put into the memory of the configuration, portrait dedication to everyone:
reference [Eaccelerator]
Eaccelerator.shm_size= "32"
Eaccelerator.cache_dir= "/tmp/eaccelerator"
eaccelerator.enable= "1"
Eaccelerator.optimizer= "1"
Eaccelerator.check_mtime= "1"
eaccelerator.debug= "0"
Eaccelerator.filter= ""
eaccelerator.shm_max= "0"
Eaccelerator.shm_ttl= "900"
eaccelerator.shm_prune_period= "1800"
eaccelerator.shm_only= "0"
eaccelerator.compress= "1"
Eaccelerator.compress_level= "9"
Eaccelerator.sessions= "SHM"
eaccelerator.keys= "SHM"
Eaccelerator.content= "SHM"



If the above two configurations are added and none can start Eaccelerator, execute the following command:
Vi/usr/local/etc/php/extensions.ini
Add the following statement at the end
Extension=eaccelerator.so

Install well, restart Apache, see Phpinfo, is not more than the following content ah.
With Eaccelerator v0.9.5, the Copyright (c) 2004-2006 eaccelerator, by Eaccelerator
This means that the installation is ready.

Here's a look at the Eaccelerator configuration options

Eaccelerator.shm_size
Specifies the amount of shared memory that Eaccelerator can use in megabytes (MB).
"0" represents the operating system default. The default value is "0".

Eaccelerator.cache_dir
Directory for user disk caching. Eaccelerator Stores precompiled code, session data, content, and so on in this directory.
The same data can also be stored in shared memory (for faster access speed). The default value is "/tmp/eaccelerator".

Eaccelerator.enable
Turn eaccelerator on or off. "1" is turned on, "0" is closed. The default value is "1".

Eaccelerator.optimizer
Turn on or off the internal optimizer to increase code execution speed. "1" is turned on, "0" is closed. The default value is "1".

Eaccelerator.debug
Turn debug logging on or off. "1" is turned on, "0" is closed. The default value is "0".

Eaccelerator.check_mtime
Turn on or off the PHP file change check. "1" is turned on, "0" is closed. If you want to recompile PHP after you modify it
The program needs to be set to "1". The default value is "1".

Eaccelerator.filter
Determine which PHP files must be cached. You can specify the cached and not cached file types (such as "*.php *.phtml", and so on)
If the arguments begin with "!", the files that match the parameters are ignored. The default value is "", that is, all PHP files
will be cached.

Eaccelerator.shm_max
Prevents large files from being stored in shared memory when the Eaccelerator_put () function is used. This parameter specifies the allowed
Maximum storage value, in bytes (10240, 10K, 1M). "0" is not limited. The default value is "0".

Eaccelerator.shm_ttl
When Eaccelerator fails to get the shared memory size of the new script, it will remove all in the shared memory from the
The last "Shm_ttl" second script cache that cannot be accessed. The default value is "0", that is: Do not remove from the shared spring
Any cached files.

Eaccelerator.shm_prune_period
When Eaccelerator fails to get the shared memory size of the new script, he will attempt to remove it from shared memory earlier than
Cache script for "shm_prune_period" seconds. The default value is "0", that is: Do not remove from the shared spring
Any cached files.

Eaccelerator.shm_only
Allows or disables caching of compiled scripts on disk. This option is not valid for session data and content caching. Default
The value is "0", that is, caching using disk and shared memory.

Eaccelerator.compress
Allows or disables the compression of content caching. The default value is "1", which is: Allow compression.

Eaccelerator.compress_level
Specifies the compression level for the content cache. The default value is "9", which is the highest level.

Eaccelerator.name_sapce
A prefix string for all keys (keys). If the prefix string is set, the. htaccess or master configuration is allowed.
The file runs two identical key names on the same host.

Eaccelerator.keys
Eaccelerator.sessions
Eaccelerator.content
Determine which keys (keys), session data, and content will be cached. The available parameter values are:
"Shm_and_disk"-caching data both in shared memory and on disk (default value);
"SHM"-if shared memory runs out or the data capacity is greater than "Eaccelerator.shm_max"
Caching data in shared memory or disk;
"Shm_only"-caching data only in shared memory;
"Disk_only"-caching data only on disk;
' None '-disables caching of data.


Eaccelerator Application Interface (API)

Eaccelerator_put ($key, $value, $ttl =0)
Stores the $value in shared memory and stores $tll seconds.

Eaccelerator_get ($key)
Returns the cached value stored by the Eaccelerator_put () function from shared memory if it does not exist or is already
Expires, NULL is returned.

EACCELERATOR_RM ($key)
Deletes the $key from shared memory.

EACCELERATOR_GC ()
Delete all expired keys (keys)

Eaccelerator_lock ($lock)
Creates a lock with the specified name. The lock can be lifted through the Eaccelerator_unlock () function, in the request
It is also unlocked automatically at the end. For example:

<?php
Eaccelerator_lock ("Count");
Eaccelerator_put ("Count", Eaccelerator_get ("Count") +1);
?>



Eaccelerator_unlock ($lock)
Unlocks the specified name (lock).

Eaccelerator_set_session_handlers ()
Installs the eaccelerator session handle.
After you have 4.2.0 from PHP, you can set the "Session.save_handler=eaacelerator" in php.ini
Install the eaccelerator handle.

Eaccelerator_cache_output ($key, $eval _code, $ttl =0)
$eval _code output is cached in shared memory, $ttl seconds are cached.
You can call the MMCACH_RM () function to delete output from the same $key. For example:

<?php eaccelerator_cache_output (' Test ', ' echo Time '); Phpinfo ();?>



Eaccelerator_cache_result ($key, $eval _code, $ttl =0)
Cache $eval _code results in shared memory, $ttl seconds cached.
You can call the MMCACH_RM () function to delete the results of the same $key. For example:

<?php eaccelerator_cache_output (' Test ', ' time () '). Hello ";",?>



Eaccelerator_cache_page ($key, $ttl =0)
The entire page is cached and $ttl seconds are cached. For example:

<?php
Eaccelerator_cache_page ($_server[' php_self '). " Get= '. Serialize ($_get), 30);
echo Time ();
Phpinfo ();
?>



Eaccelerator_rm_page ($key)
Deletes the same $key page created by the Eaccelerator_cache_page () function from the cache. The

Eaccelerator_encode ($filename)
Returns the compiled encoding of the $filename file. The

Eaccelerator_load ($code)
Loads the script encoded by the Eaccelerator_encode () function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.