# / etc / memcached.conf
#Memcached will work as a daemon
-d
# Log will add there
logfile / var / log / memcached.log
# Let's allocate 256 megabytes of RAM for storage
-m 256
# This port will listen
-p 11211
# Later it is desirable to change
-u nobody
# Listen to localhost
-l 127.0.0.1
# netstat -tap | grep memcached
tcp 0 0 localhost: 11211 * : * LISTEN 13036 / memcached
apt-get install php5-dev libmemcache-dev
pecl download memcache
tar xzvf memcache-2.2.6.tgz
cd memcache-2.2.6 /
phpize && . / configure --enable-memcache && make
cp modules / memcache.so / usr / lib / php5 / 20060613 /
echo 'extension = memcache.so' >> / etc / php5 / apache2 / php.ini
/ etc / init.d / apache2 restart
- <? php
- // Create a new object. You can also write in procedural style.
- $ memcache_obj = new Memcache ;
- // Connect to our server
- $ memcache_obj -> connect ( '127.0.0.1' , 11211 ) or die ( "Could not connect" ) ;
- // Try to get an object with our_var key
- $ var_key = @ $ memcache_obj -> get ( 'our_var' ) ;
- if ( ! empty ( $ var_key ) )
- {
- // If the object is cached, output its value.
- echo $ var_key ;
- }
- else
- {
- // If there is no object with the key our_var in the cache, create it
- // Our_var object will be stored for 5 seconds and will not be compressed
- $ memcache_obj -> set ( 'our_var' , date ( 'G: i: s' ) , false , 5 ) ;
- // Print the cached data
- echo $ memcache_obj -> get ( 'our_var' ) ;
- }
- // Close the connection to the Memcached server
- $ memcache_obj -> close ( ) ;
- ?>
- < ? php
- function LoadCPU ( )
- {
- // The function that should load the processor
- // Create 800x600 Image
- $ image = imagecreate ( 800 , 600 ) ;
- // White background color
- $ color = imagecolorallocate ( $ image, 255 , 255 , 255 ) ;
- //The black
- $ color2 = imagecolorallocate ( $ image, 0 , 0 , 0 ) ;
- for ( $ i = 0 ; $ i < 10000 ; $ i ++ ) {
- // Place 10,000 dots in random order
- imagesetpixel ( $ image, rand ( 0 , 800 ) , rand ( 0 , 600 ) , $ color2 ) ;
- }
- // Throw A Pointer
- return $ image ;
- }
- // Create a new Memcache object
- $ memcache_obj = new Memcache ;
- // Connect to our server
- $ memcache_obj - > connect ( '127.0.0.1' , 11211 ) or die ( "Could not connect" ) ;
- // Try to get the object with the key image
- $ image_bin = @ $ memcache_obj - > get ( 'image' ) ;
- if ( empty ( $ image_bin ) ) {
- // If there is no image in the cache, generate it and cache it
- imagepng ( LoadCPU ( ) , getcwd ( ) . '/tmp.png' , 9 ) ;
- $ image_bin = file_get_contents ( getcwd ( ) . '/tmp.png' ) ;
- unlink ( getcwd ( ) . '/tmp.png' ) ;
- $ memcache_obj - > set ( 'image' , $ image_bin, false , 30 ) ;
- }
- // Get the image out of the cache
- header ( 'Content-type: image / png' ) ;
- echo $ image_bin ;
- // Close the connection to the Memcached server
- $ memcache_obj - > close ( ) ;
- ? >
- <? php
- function LoadCPU ( )
- {
- // The function that should load the processor
- // Create 800x600 Image
- $ image = imagecreate ( 800 , 600 ) ;
- // White background color
- $ color = imagecolorallocate ( $ image , 255 , 255 , 255 ) ;
- //The black
- $ color2 = imagecolorallocate ( $ image , 0 , 0 , 0 ) ;
- for ( $ i = 0 ; $ i < 10000 ; $ i ++ ) {
- // Place 10,000 dots in random order
- imagesetpixel ( $ image , rand ( 0 , 800 ) , rand ( 0 , 600 ) , $ color2 ) ;
- }
- // Throw A Pointer
- return $ image ;
- }
- // Display the image without caching
- header ( 'Content-type: image / png' ) ;
- imagepng ( LoadCPU ( ) , '' , 9 ) ;
- ?>
Source: https://habr.com/ru/post/108274/
All Articles