How do Inserts in Redis using Python | Python Redis Example

How do Inserts in Redis using Python

How do Inserts in Redis using Python | Python Redis Example: In this article, we will learn a little more about Redis an engine that records key and value using atomic operations ( one at a time ).

Introduction

For those who do not know, Redis is an in-memory, key-value data store. This means that Redis is an engine that records key and value as a Memcached, but persistent. It supports data in String, Integers, Lists, Sets, Ordered Sets and Hash Tables; and also makes the use of atomic operations (a change at a time), and has the methods: get / set, push / pop, add / remove, union, intersect, diffs, among others, including sorting options.



Redis also maintains, by default, all data in memory, and saves snapshots to disk asynchronously or writing each change to an “Append the Only File”. Some high-profile companies in the market have joined the Redis as GitHub, Tweetdeck, Flickr, StackOverflow, among others. You can have access to a complete list in this link.

To begin with, we must first have Redis installed on our environment. If you have an OS X or Linux environment, just run the following commands:

Listing 1: Installing Redis

$ wget http://redis.googlecode.com/files/redis-2.4.16.tar.gz
$ tar xzf redis-2.4.16.tar.gz
$ cd redis-2.4.16
$ make

Soon after run these commands, see if you can run Redis:

Listing 2: Running Redis

$ src/redis-server
You should see something like: [ 10831 ] Aug 09 23:09:37 * Server started, Redis version 2.4.16

If you are running under Windows, visit the official website of Redis and download the executable.

Now it’s time to play around! Let’s make a basic listing in our local Redis using Python. For this, we need the lib Redis for language. You can check if you already have this lib into the interactive Python shell and be importing the same.

Note: You indicated to create a virtual environment for the installation of any Python library on your machine. To do this, go to the virtualenv documentation and follow the installation instructions.

Listing 3: Installing lib Redis

$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import redis
>>>

If you receive no errors, congratulations! You already have the lib installed Redis. If not, install it by pip ( package manager Python ).

Listing 4: Installing the pip ( package manager Python ).

$ pip install redis
Ready! Now that you’ve installed everything we need, we will finally make a local insertion in Redis.

We connect to our local bank, and then we will do the following:

Listing 5: Connecting the pens to the local database

import redis
redis = redis.Redis(‘localhost’)

Note: In our example, we will use a database running locally on our machine ( localhost ), but the idea for the database on external servers is the same.

Ready! We’re connected to the Redis locally. The library Redis for Python provides very easy methods to interact, so if we want to insert a key/value , basically ran the following command:

Listing 6 : Inserting a key / value in pens
redis.set(‘foo’,’bar’)
To test if it really worked, let’s run this other command:

Listing 7: Testing if everything is working
print redis.get(‘foo’)

If the result is ‘bar’, great! It is already working and you can play entering data in your pens!

Now we learn to work with Strings in Redis , we will work with other types of data :
lists

Working with lists in Redis is very similar to working with them in Python. Some methods using lists:

Listing 8 : Working with lists in Redis
redis.rpush ( ‘ tools ‘, ‘ hammer ‘ )
redis.rpush ( ‘ tools ‘, ‘ Hacksaw ‘ )
redis.rpush ( ‘ tools ‘ ‘ ax ‘ )

redis.lrange ( ‘ ferammentas ‘, 0 , -1 )
// Hammer, saw , ax

redis.llen ( ‘ tools ‘ )
// 3

redis.lindex ( ‘ tools ‘, 1 )
// hammer

Sets
The Sets are also very similar to the structure of lists in Python, however , this list may not contain the same values. The elements are unique. Example:

Listing 9 : Working with Sets in Redis
redis.delete ( ‘ tools ‘ )
// True

redis.sadd ( ‘ tools ‘, ‘ hammer ‘ )
// True

redis.sadd ( ‘ tools ‘, ‘ Hacksaw ‘ )
// True

redis.sadd ( ‘ tools ‘ ‘ ax ‘ )
// True

redis.sadd ( ‘ tools ‘, ‘ hammer ‘ )
// False

redis.smembers ( ‘ tools ‘ )
// Set ( [ ‘ Hacksaw ‘ ‘ ax ‘, ‘ hammer ‘] )

This data structure is often used when you need to validate an entry as a voting system .

sets ordered

Finally , we have the ordered sets. They are like normal sets, but with a additional attribute called “score ” . This attribute defines the order of the element in the set.

Listing 10: Working with Sets ordered in Redis
redis.zadd(‘test:scores’, ‘testid:9000’, 50)
// True

redis.zadd(‘test:scores’, ‘testid:8000’, 20)
// True

redis.zadd(‘test:scores’, ‘testid:7000’, 45)
// True

redis.zadd(‘test:scores’, ‘testid:7000’, 45)
//False

redis.zrange(‘test:scores’, 0, -1, withscores=True)
// [(‘testid:7000’, 30.0), (‘testid:8000’, 40.0), (‘testid:9000’, 50.0)]

With this, we have our ordered list working, and we can also reverse it easily , just use the reverse () command , see the example below :

Listing 11 : Reverting an ordered list in Redis
test = redis.zrange(‘test:scores’, 0, -1, withscores=True)
test.reverse()
test
// [(‘testid:9000’, 50.0), (‘testid:8000’, 40.0), (‘testid:7000’, 30.0)]

Well, that’s it! Now we can work a little bit with Redis and Python .

If you want to know more about the other methods that can be used, visit the official documentation and guarantees hours of fun with this amazing tool!

For today I am here with this article on Redis and Python, I hope you enjoyed the article and to the next.
How do inserts in Redis using Python

In this article, we will learn a little more about Redis an engine that records key and value using atomic operations ( one at a time ).

Introduction

For those who do not know, Redis is an in-memory, key-value data store. This means that Redis is an engine that records key and value as a Memcached, but persistent. It supports data in String, Integers, Lists, Sets, Ordered Sets and Hash Tables; and also makes the use of atomic operations (a change at a time), and has the methods: get / set, push / pop, add / remove, union, intersect, diffs, among others, including sorting options.

Redis also maintains, by default, all data in memory, and saves snapshots to disk asynchronously or writing each change to an “Append the Only File”. Some high-profile companies in the market have joined the Redis as GitHub, Tweetdeck, Flickr, StackOverflow, among others. You can have access to a complete list in this link.

To begin with, we must first have Redis installed on our environment. If you have an OS X or Linux environment, just run the following commands:

Listing 1: Installing Redis

$ wget http://redis.googlecode.com/files/redis-2.4.16.tar.gz
$ tar xzf redis-2.4.16.tar.gz
$ cd redis-2.4.16
$ make

Soon after run these commands, see if you can run Redis:

Listing 2: Running Redis

$ src/redis-server
You should see something like: [ 10831 ] Aug 09 23:09:37 * Server started, Redis version 2.4.16

If you are running under Windows, visit the official website of Redis and download the executable.

Now it’s time to play around! Let’s make a basic listing in our local Redis using Python. For this, we need the lib Redis for language. You can check if you already have this lib into the interactive Python shell and be importing the same.

Note: You indicated to create a virtual environment for the installation of any Python library on your machine. To do this, go to the virtualenv documentation and follow the installation instructions.

Listing 3: Installing lib Redis

$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import redis
>>>

If you receive no errors, congratulations! You already have the lib installed Redis. If not, install it by pip ( package manager Python ).

Listing 4: Installing the pip ( package manager Python ).

$ pip install Redis
Ready! Now that you’ve installed everything we need, we will finally make a local insertion in Redis.

We connect to our local bank, and then we will do the following:

Listing 5: Connecting the pens to the local database

import redis
redis = redis.Redis(‘localhost’)

Note: In our example, we will use a database running locally on our machine ( localhost), but the idea for the database on external servers is the same.

Ready! We’re connected to the Redis locally. The library Redis for Python provides very easy methods to interact, so if we want to insert a key/value, basically ran the following command :

Listing 6 : Inserting a key / value in pens
redis.set(‘foo’,’bar’)
To test if it really worked, let’s run this other command:

Listing 7 : Testing if everything is working
print redis.get(‘foo’)

If the result is ‘bar’ , great! It is already working and you can play entering data in your pens !

Now we learn to work with Strings in Redis , we will work with other types of data :
lists

Working with lists in Redis is very similar to working with them in Python. Some methods using lists:

Listing 8 : Working with lists in Redis
redis.rpush ( ‘ tools ‘, ‘ hammer ‘ )
redis.rpush ( ‘ tools ‘, ‘ Hacksaw ‘ )
redis.rpush ( ‘ tools ‘ ‘ ax ‘ )

redis.lrange ( ‘ ferammentas ‘, 0 , -1 )
// Hammer, saw , ax

redis.llen ( ‘ tools ‘ )
// 3

redis.lindex ( ‘ tools ‘, 1 )
// hammer

Sets
The Sets are also very similar to the structure of lists in Python, however , this list may not contain the same values. The elements are unique. Example:

Listing 9 : Working with Sets in Redis
redis.delete ( ‘ tools ‘ )
// True

redis.sadd ( ‘ tools ‘, ‘ hammer ‘ )
// True

redis.sadd ( ‘ tools ‘, ‘ Hacksaw ‘ )
// True

redis.sadd ( ‘ tools ‘ ‘ ax ‘ )
// True

redis.sadd ( ‘ tools ‘, ‘ hammer ‘ )
// False

redis.smembers ( ‘ tools ‘ )
// Set ( [ ‘ Hacksaw ‘ ‘ ax ‘, ‘ hammer ‘] )

This data structure is often used when you need to validate an entry as a voting system .

sets ordered

Finally , we have the ordered sets. They are like normal sets, but with a additional attribute called “score ” . This attribute defines the order of the element in the set.

Listing 10: Working with Sets ordered in Redis
redis.zadd(‘test:scores’, ‘testid:9000’, 50)
// True

redis.zadd(‘test:scores’, ‘testid:8000’, 20)
// True

redis.zadd(‘test:scores’, ‘testid:7000’, 45)
// True

redis.zadd(‘test:scores’, ‘testid:7000’, 45)
//False

redis.zrange(‘test:scores’, 0, -1, withscores=True)
// [(‘testid:7000’, 30.0), (‘testid:8000’, 40.0), (‘testid:9000’, 50.0)]

With this, we have our ordered list working, and we can also reverse it easily , just use the reverse () command , see the example below :

Listing 11 : Reverting an ordered list in Redis
test = redis.zrange(‘test:scores’, 0, -1, withscores=True)
test.reverse()
test
// [(‘testid:9000’, 50.0), (‘testid:8000’, 40.0), (‘testid:7000’, 30.0)]

Well, that’s it! Now we can work a little bit with Redis and Python .

If you want to know more about the other methods that can be used , visit the official documentation and guarantees hours of fun with this amazing tool !

For today I am here with this article on Redis and Python , I hope you enjoyed the article and to the next .




LEAVE A REPLY

Please enter your comment!
Please enter your name here