Using Redis in PHP - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Tuesday, June 30, 2020

Using Redis in PHP


PHP-(Hypertext-Pre-processor) which earlier meant as Personal Home Pages. 
PHP is used for developing websites ( static or dynamic), or developing web applications.
PHP is  one of the most popular scripting languages, which is used to enhance web pages, With the PHP, you can do things like create a user name and password login pages, check details from a form, create forums, picture galleries, surveys, and a whole lot more.
Redis -(/ Ëˆ r É› d Éª s /; Remote Dictionary Server) is in-memory data structure store, used as a database, cache, and message broker. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indexes.
Redis client libraries are available for many languages and are listed on the Redis website, For PHP, there are five, and for this article, we will be using the Predis library.
Redis in PHP is used to store and manipulate complex data structures in memory. Redis provides a simple way of storing these data.

Installation : ( Redis and client -Predis)

Follow the link for easy setup and installation

https://www.udemy.com/course/redis-php/
Redis has built-in data types, these datatypes help developers store data in a more meaningful manner and perform operations which is much faster, when the data provided.
Redis Data Types:
String – The basic data type used in Redis in which we can store from  characters to the content of an entire file, for instance, a JPEG image or a serialized Ruby object. Redis Strings are binary safe, this means that a Redis string can contain any kind of data.
A String value can be at max 512 Megabytes in length.
You can do a number of interesting things using strings in Redis, for instance, you can:
Use Strings as atomic counters using commands in the INCR family: 
INCR, DECR, INCRBY.
Append to strings with the APPEND command.
Use Strings as random access vectors with GETRANGE and SETRANGE.
Encode a lot of data in little space, or create a Redis backed Bloom Filter using GETBIT and SETBIT.
List – Lists are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list.
A new list is created when one of these operations is performed against an empty key. Similarly, the key is removed from the keyspace if a list operation will empty the list. These are very handy semantics since all the list commands will behave exactly like they were called with an empty list if called with a non-existing key as argument.
Some example of the list operations and resulting lists:
LPUSH mylist x   # now the list is "x"
LPUSH mylist y   # now the list is "y","x"
RPUSH mylist z   # now the list is "y","x","z" (RPUSH was used this time)
 A simple list of strings order by the insertion of its elements. You can add and remove elements from both the list’s head and tail, so you can use this data type to implement queues.
Set – An unordered collection of strings where we can add, remove, and test for existence of members, there is   one constraint is that we are not allowed to have repeated members.
You can track unique things using Redis Sets. Want to know all the unique IP addresses visiting a given blog post? Simply use SADD every time you process a page view. You are sure repeated IPs will not be inserted.
Redis Sets are good to represent relations. You can create a tagging system with Redis using a Set to represent every tag. Then you can add all the IDs of all the objects having a given tag into a Set representing this particular tag, using the SADD command. Do you want all the IDs of all the Objects having three different tags at the same time? Just use SINTER.
You can use Sets to extract elements at random using the SPOP or SRANDMEMBER commands.
Hash – Hash is a map between string keys and string values. In this way you can represent objects, A hash with a few fields  is stored in a way that takes very little space (where few means up to one hundred or so), so you can store millions of objects in a small Redis instance.
While Hashes are used mainly to represent objects, they are capable of storing many elements, so you can use Hashes for many other tasks as well.
(e.g. A User with a number of fields like name, age, and so forth):
HMSET user:100 username bhawna password Asdfg age 37
HGETALL user:100
HSET user:100 password 54321
HGETALL user:100
Sorted set – A particular case of the set data type, which is similar to String set with non-repeated strings, every member is associated with a score, which is used the order from smallest to largest score.
With sorted sets, you can add, remove, or update elements in a very fast way.
With Sorted Sets you can:
Take a leaderboard in a computer game, where every time a new score is submitted you update it using ZADD. You can easily take the top users using ZRANGE, you can also, give a user name, return its rank in the listing using ZRANK

Using ZRANK and ZRANGE together you can show users with a score similar to a given user. All very quickly.

Advantages of using Redis

Redis uses its own hashtag mechanism called Redis hashtag. Redis stored data in the form of a key and a map.

Data Replication – Replication means to setup master-slave cache nodes. The slave nodes always listen to the master.
In this, when the master is updated slaves automatically be updated as well.

Redis has clients in all the popular programming languages-Redis have client API developed in all famous languages such as C, Java, Javascript, Python.

Redis allows inserting a huge amount of data into the cache easily-when working with data sometimes it is required to load millions of pieces of data into the cache within a short period of time. This can be easily done with mass insertion in Redis

Redis protocol makes it simple to implement –Redis client communicates with its server using RESP(Redis Serialization Protocol). These protocols are simple to read and understand.

By now, you know what is Redis, its types, simple declarations, installing, and advantages.

To explore these topics in detail you can visit ;






No comments:

Post a Comment

Post Top Ad