Using Memcache for php

Using Memcache for PHP

This article will explain the process of using memcache for php dynamic websites. Memcache is used to implement cache mechanism using PHP. There are also many cache engines available for PHP, but using memcache for PHP will provide some additional benefits.

Memcache itself providing procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

Below are the requirements that has to be accomplished before going to explain the process of using memcache for PHP.

1) Memcache service has to be installed and started. For windows operation system read this page and for Ubuntu read this page

2) Memcache php extension is installed and enabled to run with PHP. To know installation process read this page.

Considering both Memcache service and Memcache php extension are started and working fine, here we go with Using Memcache for PHP explanation.

Here’s some sample code from php.net site

<?php
  $memcache = new Memcache;
  $memcache->connect("localhost",11211); # You might need to set "localhost" to "127.0.0.1"
  echo "Server's version: " . $memcache->getVersion() . "<br />\n";
  $tmp_object = new stdClass;
  $tmp_object->str_attr = "test";
  $tmp_object->int_attr = 123;
  $memcache->set("key",$tmp_object,false,10);
  echo "Store data in the cache (data will expire in 10 seconds)<br />\n";
  echo "Data from the cache:<br />\n";
  var_dump($memcache->get("key"));

?>

If you have any errors, it will most likely be because the php extension didn’t run. Check the webserver error log to see if it had problems starting. The most common problem is the version of memcache.dll is not compatible with the version of php you are running.

Installing Memcache PHP Extension

Installing Memcache PHP Extension

Installing Memcache PHP extension article will explain how to install Memcache dll file both in Xampp and Wamp servers.

Follow the below steps for Installing Memcache PHP Extension in Wamp or Xampp

1) Download the memcache php extension zip file form this link

2) Unzip and find the php_memcache.dll file.

3) For Xampp server place the dll file in D:\xampp\php\ext folder. Here considering that Xampp is installed in D Drive.

4) For Wamp server place the dll file in D:\wamp\bin\php\php5.2.9-1\ext folder. Here considering that Wamp is installed in D Drive.

5) Open the php.ini file which is located at D:\xampp\php  or D:\wamp\bin\php\php5.2.9-1\  for Xampp or Wamp servers respectively

6) Find and replace

;extension=php_memcache.dll

with

extension=php_memcache.dll

7) If you cannot find this line simply add the following to the extensions list

extension=php_memcache.dll

8) Restart the server ( Wamp/Xampp)

To check the Installation is success or not

  • Please create a php file (phpinfo.php) some where in the wamp/xampp.
  • Run that file through browser.
  • Search for Memcache in the page .
  • If you can see it, means the installation is success and is working fine.

Note : If you have any errors, it will most likely be because the php extension didn’t run. Check the webserver error log to see if it had problems starting. The most common problem is the version of memcache.dll is not compatible with the version of php you are running.

Read Installation of Memcached service on Windows here

Read Installation of Memcached service on Ubuntu here

Memcached Installation on Windows

Memcached Installation on Windows

Here in this article ‘Memcached Installation on Windows’ we are going to see how to install Memcached on Windows.
As we know Memcached is a Free & open source, high-performance, distributed memory object caching system , generic in nature, but intended for use in speeding up dynamic web applications.

Steps of Memcached installation on windows are given below

Step 1 :

  •  First of all we need to download Memcahced Installation / Service files based on the operation system bits ( Windows 32 bit OR windows 64 bit) .
  •  For Windows 32 Bit operation system, use the win32 binary version. You can download it form the here 
  •  For Windows 64 Bit operation system, use the win64 binary version. You can download it form here OR from here

Step 2 :

  •  Unzip the downloaded zip file to find the file named memcached.exe.
  • Create a folder with name memcached in any drive.Prefer the drive (C Drive) in which the operating system installed.
  •  Copy memcahced.exe file from the unziped folder to the newly created folder ( C:\memcached )

Step 3 :

Install the Memcached service. This can be done in two ways

  1. Right click on memcached.exe file and Run as administrator .
  2. Find the cmd.exe in the system, right hand click on the icon and select Run as administrator

Step 4 :

Open the command line interface and go to the folder where memcached.exe file is available.

C:\memcached>

Run the below commands to install the Memcached service and start it.

C:\memcached>memcached.exe -d install

C:\memcached>memcached.exe -d start

If you dont get any errors it means it’s worked.

If  you face some problem. It’s may shows some error like “The program can’t install because MSVCR71.dll is missing from your computer .“

To solve this issue you need to download the MSVCR71.dll file. You can download it from here. Download the dll and paste it inside your system 32 folder which is in C:\Windows\system32  and again go to the command prompt and follow the Step 4. Now it should not throw error.

 

Installation of Memcached on Ubuntu

Read about memcache and cakephp here

set up memcache engine for cakephp

Set up memcache engine for cakephp

Here in this article we are going to see how to set up Memcache Engine for cakephp. Memcached is a Free & open source, high-performance, distributed memory object caching system , generic in nature, but intended for use in speeding up dynamic web applications.

CakePHP comes with several cache engines built-in, and provides an easy system to implement our own caching systems.By default it provides file based caching mechanism which uses local files to store the cached content and is slowest cache engine, and doesn’t provide as many features for atomic operations.

Since the default file engine is very slow there comes the search of alternative cache engines that cackephp provides. one of the most fast search engine that comes in cakephp is Memcache Engine. Below are the steps to set up Memcache Engine for cakephp.

1) Installing the Memcached in operating system.

2) Installing Memcache module/extension for php.

3) Restarting the Apache server.

4) Making Memcache Engine as default engine for cakephp App.

  • Open the bootstrap file which will find in ‘root_folder/app/config/bootstrap.php’
  • Find and replace the code.
Cache::config('default', array('engine' => 'File'));

With

Cache::config('default', array(
    'engine' => 'Memcache',
    'duration' => 3600, // 
    'probability' => 100,
    'prefix' => Inflector::slug(APP_DIR) . '_',
    'servers' => array(
        '127.0.0.1'
    ),
    'persistent' => true,
    'compress' => false,
));

5) Set  Memcache engine as the main caching engine for the app

  • Open the core file which will find in ‘root_folder/app/config/core.php’
  • Find and replace the code.
$engine = 'File';

With

$engine = 'Memcache';

Read more about memcache here

sugarcrm change password validation rules

sugarcrm change password validation rules

In SugarCRM there are some default validation rules that has to be followed while creating or resetting the password. This article  sugarcrm change password validation rules  will explain how to change the default validation rules provided by SugarCRM.

The validation rules are defined in the config file/setting and the  minimum validation rules that are available/required for password are given below.

1) At least one uppercase
2) At least one numeric.

sugarcrm change password validation rules, to change/modify the above mentioned validation rules we have to edit configuration settings. To do so open the config_override.php available in root folder and make the changes as per the requirement.

To change the validation rules, Please edit the below array elements

$sugar_config[‘passwordsetting’][‘oneupper’] = false;   // which removes the validation rule ( one Upper case letter in password )
$sugar_config[‘passwordsetting’][‘onenumber’] = false;  // which removes the validation rule ( one number in password )

To read more about sugarCRM click here

 

sugarcrm change password validation rules

php group numbers into intervals

php group numbers into intervals

The article php group numbers into intervals will explain how to group the provided numbers in to intervals.

 

function group_nums($array) {
  $ret = array();
  $temp = array();
  foreach($array as $val) {
    if(next($array) == ($val + 1))
      $temp[] = $val;
    else
      if(count($temp) > 0) {
        $temp[] = $val;
        $ret[] = $temp[0].’:’.end($temp);
        $temp = array();
      }
      else
        $ret[] = $val;
    }
  return $ret;
}

$nums = array(1, 2, 3, 4, 14, 21, 23, 28, 29);
print_r(group_nums($nums));

// OUTPUT 
Array
(
    [0] => 1-4
    [1] => 14
    [2] => 21
    [3] => 23
    [4] => 28-29
)

 

Get users who flagged a node

Get users who flagged a node

This article will explain how to get users who flagged a node in drupal. Drupal provides a contributed module named Flag module which is used to create unlimited arbitrary flags or bookmarks to flag content.

In one for the Drupal project we had a requirement to get users who flagged a node and display their details when ever that node is viewed in the browser.

To get users who flagged a node, here node can be any thing like blog post, user, content type etc… we need to invoke flog module in our custom module or block that has to display the users list.

Use below code to get all the users who had flagged a particular node.

Step 1 :  To get the node ID from URL

if (arg(0) == 'node') {
  $nid = arg(1);
}

Step 2 :  Using node ID get users who flagged a node.

$user_ids=array(); //User Array initialization 
$nid = 2; //node ID
  $flags = module_invoke('flag', 'get_content_flags', 'node', $nid);
  if (isset($flags)) {
    foreach ($flag as $uid => $data) {
       $user[] = $uid; //Adds user ids to the user array.
    }
  }

To get User pictures user below code

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  $nid = $node->nid;
  $accounts = module_invoke('flag', 'get_content_flags', 'node', $nid);
  if (isset($accounts)) {
    foreach ($accounts as $uid => $data) {
      $account = user_load(array('uid' => $uid));
      print theme('user_picture', $account); //Substitute this with whatever you'd like to display from the user.
    }
  }
}

To read more about Drupal from here

Drupal get comments of node

Drupal get comments of node

In Drupal 7 you can use the below code to get all the comments of a node using node ID

$nid = 2; // node ID
    $comments = db_select('comment')
              ->fields('comment', array('name','subject'))
              ->condition('nid', $nid, '=')
              ->execute()
              ->fetchAssoc();
    foreach($comments as $comment) {
        print your comments here
    }

In Drupal 6 you can use the comment_render() ;

 

 

Database connection with Nodejs and print data to browser

Install Node.js

Open Command prompt  start server

Run below command in command prompt
npm install mysql

var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'hostname',
user: 'username',
password:'password',
database: 'dbname',
port: 3306
});

console.log('MySQL Connection details  '+connection);

http.createServer(function (request, response)
{
console.log('Creating the http server');
connection.query('SELECT * FROM user', function(err, rows, fields)
{
console.log('Connection result error '+err);
console.log('no of records is '+rows.length);
response.writeHead(200, { 'Content-Type': 'application/json'});
response.end(JSON.stringify(rows));
response.end();
});

}).listen(8084);

Memcache Installation in Ubuntu

Memcache Installation in Ubuntu

This article explains about installation of Memcache in Ubuntu systems.

Memcache installation in Ubuntu is required to use chache mechanism in the applications developed using php. Many frames woks and CMS developed using php can use the advantages of Memcache.

Below are the steps used to follow for Memcahe installation in ubuntu system.

Ensure that we will be downloading the latest packages to our server:

sudo apt-get update

Install memcached:

sudo apt-get install memcached php5-memcache


Install php-pear which is the repository which stores memcache:

sudo apt-get install php-pear


Unless you already have a compiler on your server,  download build-essential:

sudo apt-get install build-essential


Use PECL  to install memcache:

sudo pecl install memcache

Respond “yes” if asked whether you would like to enable memcache session handler support.
Finally, add memcache to php.ini

sudo echo “extension=memcache.so” > sudo /etc/php5/apache2/php.ini

checkk it terminal by typing command
–>telnet localhost 11211
add extension in php.ini
–>extension=php_memcache.dll
restart apache
–> sudo /etc/init.d/apache2 start
check in phpinfo.php file memcache enabled