Redis database (Elementary)

Source: Internet
Author: User
Tags redis cluster install redis

Redis database (Elementary)

Redis is an open-source non-relational database. It is written in C language and is a key-value storage system. It stores many value types, including string, list ), set, zset, and hash ).

For example, we insert a data record as follows:

Import redis

Class Database:
Def _ init _ (self ):
Self. host = 'localhost'
Self. port = 6379

Def write (self, website, city, year, month, day, deal_number ):
Try:
Key = '_'. join ([website, city, str (year), str (month), str (day)])
Val = deal_number
R = redis. StrictRedis (host = self. host, port = self. port)
R. set (key, val)
Failed t Exception, exception:
Print exception

Def read (self, website, city, year, month, day ):
Try:
Key = '_'. join ([website, city, str (year), str (month), str (day)])
R = redis. StrictRedis (host = self. host, port = self. port)
Value = r. get (key)
Print value
Return value
Failed t Exception, exception:
Print exception

If _ name _ = '_ main __':
Db = Database ()
Db. write ('meituany', 'beijing)
Db. read ('meituany', 'beijing', 1)

The above operation writes a piece of data first and then reads it. If too much data is written or read, we 'd better use batch processing to improve efficiency.

Import redis
Import datetime

Class Database:
Def _ init _ (self ):
Self. host = 'localhost'
Self. port = 6379
Self. write_pool = {}

Def add_write (self, website, city, year, month, day, deal_number ):
Key = '_'. join ([website, city, str (year), str (month), str (day)])
Val = deal_number
Self. write_pool [key] = val

Def batch_write (self ):
Try:
R = redis. StrictRedis (host = self. host, port = self. port)
R. mset (self. write_pool)
Failed t Exception, exception:
Print exception


Def add_data ():
Beg = datetime. datetime. now ()
Db = Database ()
For I in range (1,10000 ):
Db. add_write ('meituany', 'beijing', 2013, I, 1, I)
Db. batch_write ()
End = datetime. datetime. now ()
Print end-beg

If _ name _ = '_ main __':
Add_data ()

Install and test Redis in Ubuntu 14.04

Redis cluster details

Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis

Redis series-installation, deployment, and maintenance

Install Redis in CentOS 6.3

Learning notes on Redis installation and deployment

Redis. conf

Redis details: click here
Redis: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.