Schogini is Magento2 ready. Are you?

Get started with Magento 2 | Need expert Magento2 support?

Super Quick Redis and PHP Howto for the Super Busy


Category: Magento2

Redis server is a key/value store (associative array) which fast and works in memory. Like a MySQL Server we can connect to a Redis server using socket connection to it's port (usually 6379)

How to Install Redis on a Mac

To install Redis on other operating systems like Windows and Linux please search the net for it. On a Mac install Home Brew if not already installed. Then.. sudo brew install redis You may be promoted for Xcode command line tools/license etc. Follow the on screen instructions.

These ways of launching the Redis Server

  • To have launchd start redis at login

    • ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
  • Then to load redis now:

    • launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
  • Or, if you don't want/need launchctl, you can just run:

    • sudo redis-server /usr/local/etc/redis.conf I suggest first try the last option to manually start the Redis Server.

Assuming that you have started we can test if the server is running by pinging it. redis-cli ping PONG If you get a PONG response it confirms that the Redis Server is up and running.

To come to Redis Server's command line prompt using the client interface type. redis-cli 127.0.0.1:6379>

How to use RedisLabs.com Redis Server instead of the locally hosted Redis.

Signup at http://redislabs.com and choose the free 30MB account. Once you have created a new subscription you will get an end point. Using that you can connect to the server as below. redis-cli -h AAABBBCCCDDD.us-east-1-2.2.ec2.garantiadata.com -p 18839 AAABBBCCCDDD.us-east-1-2.2.ec2.garantiadata.com:18839> auth PASSWORD AAABBBCCCDDD.us-east-1-2.2.ec2.garantiadata.com:18839>

How to try some Redis Commands

Type help @ and press tab key to cycle through various command groups. ``` 127.0.0.1:6379> help @generic @string @list @set @sorted_set @hash @pubsub @transactions @connection @server @scripting @hyperloglog

Eg: Help @Server will list

127.0.0.1:6379> help @server

BGREWRITEAOF -

summary: Asynchronously rewrite the append-only file

since: 1.0.0

BGSAVE -

summary: Asynchronously save the dataset to disk

since: 1.0.0

CLIENT GETNAME -

summary: Get the current connection name

since: 2.6.9

CLIENT KILL ip:port

summary: Kill the connection of a client

since: 2.4.0

CLIENT LIST -

summary: Get the list of client connections

since: 2.4.0

CLIENT PAUSE timeout

summary: Stop processing commands from clients for some time

since: 2.9.50

CLIENT SETNAME connection-name

summary: Set the current connection name

since: 2.6.9

CONFIG GET parameter

summary: Get the value of a configuration parameter

since: 2.0.0

CONFIG RESETSTAT -

summary: Reset the stats returned by INFO

since: 2.0.0

CONFIG REWRITE -

summary: Rewrite the configuration file with the in memory configuration

since: 2.8.0

CONFIG SET parameter value

summary: Set a configuration parameter to the given value

since: 2.0.0

DBSIZE -

summary: Return the number of keys in the selected database

since: 1.0.0

DEBUG OBJECT key

summary: Get debugging information about a key

since: 1.0.0

DEBUG SEGFAULT -

summary: Make the server crash

since: 1.0.0

FLUSHALL -

summary: Remove all keys from all databases

since: 1.0.0

FLUSHDB -

summary: Remove all keys from the current database

since: 1.0.0

INFO [section]

summary: Get information and statistics about the server

since: 1.0.0

LASTSAVE -

summary: Get the UNIX time stamp of the last successful save to disk

since: 1.0.0

MONITOR -

summary: Listen for all requests received by the server in real time

since: 1.0.0

SAVE -

summary: Synchronously save the dataset to disk

since: 1.0.0

SHUTDOWN [NOSAVE] [SAVE]

summary: Synchronously save the dataset to disk and then shut down the server

since: 1.0.0

SLAVEOF host port

summary: Make the server a slave of another instance, or promote it as master

since: 1.0.0

SLOWLOG subcommand [argument]

summary: Manages the Redis slow queries log

since: 2.2.12

SYNC -

summary: Internal command used for replication

since: 1.0.0

TIME -

summary: Return the current server time

since: 2.6.0

DEL key [key ...]

summary: Delete a key

since: 1.0.0

DUMP key

summary: Return a serialized version of the value stored at the specified key.

since: 2.6.0

EXISTS key

summary: Determine if a key exists

since: 1.0.0

EXPIRE key seconds

summary: Set a key's time to live in seconds

since: 1.0.0

EXPIREAT key timestamp

summary: Set the expiration for a key as a UNIX timestamp

since: 1.2.0

KEYS pattern

summary: Find all keys matching the given pattern

since: 1.0.0

MIGRATE host port key destination-db timeout [COPY] [REPLACE]

summary: Atomically transfer a key from a Redis instance to another one.

since: 2.6.0

MOVE key db

summary: Move a key to another database

since: 1.0.0

OBJECT subcommand [arguments [arguments ...]]

summary: Inspect the internals of Redis objects

since: 2.2.3

PERSIST key

summary: Remove the expiration from a key

since: 2.2.0

PEXPIRE key milliseconds

summary: Set a key's time to live in milliseconds

since: 2.6.0

PEXPIREAT key milliseconds-timestamp

summary: Set the expiration for a key as a UNIX timestamp specified in milliseconds

since: 2.6.0

PTTL key

summary: Get the time to live for a key in milliseconds

since: 2.6.0

RANDOMKEY -

summary: Return a random key from the keyspace

since: 1.0.0

RENAME key newkey

summary: Rename a key

since: 1.0.0

RENAMENX key newkey

summary: Rename a key, only if the new key does not exist

since: 1.0.0

RESTORE key ttl serialized-value

summary: Create a key using the provided serialized value, previously obtained using DUMP.

since: 2.6.0

SCAN cursor [MATCH pattern] [COUNT count]

summary: Incrementally iterate the keys space

since: 2.8.0

SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]

summary: Sort the elements in a list, set or sorted set

since: 1.0.0

TTL key

summary: Get the time to live for a key

since: 1.0.0

TYPE key

summary: Determine the type stored at key

since: 1.0.0

Installing PHP Redis Library on a Mac running XAMPP

  cd ~
  git clone https://github.com/nicolasff/phpredis
  cd phpredis/phpize
  ./configure 
  make
  make install
  sudo make install
  Installing shared extensions: /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20131226/

Sample PHP code to test the Redis Server

  <?php
   //Connecting to a Redis server on runing on localhost port 6379
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connected sucessfully\n";
   //Ping the server
   echo "Pinging: "+ $redis->ping() + "\n";
  ?>

Output should look like this

Connected sucessfully 0

<?php
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   //store data as a redis list     
   $redis->lpush("Schogini", "Magento");
   $redis->lpush("Schogini", "Magento 2");
   // Print data
   $arList = $redis->lrange("Schogini", 0 ,1);
   print_r($arList);
?>

Out should look like this

Array
(
    [0] => Magento 2
    [1] => Magento
)
<?php
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   // Print keys
   $arList = $redis->keys("*");
   print_r($arList);
?>

Out should look like this

Array
(
    [0] => Schogini
)

Connecting to our remote Redis server <?php $redis = new Redis(); $redis->connect('AAABBBCCCDDD.us-east-1-2.2.ec2.garantiadata.com', 18839); //store data as a redis list $redis->auth('PASSWORD'); $redis->lpush("Schogini", "Magento"); $redis->lpush("Schogini", "Magento 2"); $redis->lpush("Schogini 2", "Magento 2"); // Print data $arList = $redis->lrange("Schogini", 0 ,1); print_r($arList); ?> Out should look like this Array ( [0] => Magento 2 [1] => Magento )


 

Schogini's customer service extends beyond the natural boundaries. ~ Timothy Johnson

Schogini assures 24x7 - 6 hour turn around ticket response time and 24 hour Email response time (support@schogini.com)