In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Transferred from: http://www.diybl.com/course/3_program/python/20110510/555370.html
# 1. Installation
Well, I admit I only know how to install the simplest.
Sudo apt-get install redis-server
Python support package: (actually only one file, you can use it when you get it)
Sudo apt-get install python-redis
# 2. Configuration
Configure it. The default configuration file is "/ etc/redis/redis.conf"
Bind ip:
"bind 127.0.0.1"-> "bind 10.0.1.7"
Changing disk synchronization to out-of-sync or per-second synchronization is too slow to keep synchronizing:
"appendfsync always"-> "appendfsync no"
Check whether the background execution is turned on:
"daemonize yes"
Or anything else you want to set, such as:
Connection timeout: "timeout 300"
Running level: "loglevel notice" (personally, I think the default one is good. If there is no big exception, there is no need to change it to debug)
# 3. Use
#! / usr/bin/env python
# coding=utf-8
Import redis
Print redis.__file__
# connection, optional for different databases
R = redis.Redis (host='10.0.1.7', port=6379, db=0)
#
# look at the message
Info = r.info ()
For key in info:
Print's'% s'% (key, info [key])
# check database size
Print'\ ndbsize:% s'% r.dbsize ()
# look at the connection
Print's "ping" r.ping ()
# Select a database
# r.select (2)
# move data to 2 database
# r.move ('axioma2)
# other
# r.save ('a') # storing data
# r.lastsave ('a') # take the last save time
# r.flush () # Refresh
# r.shutdown () # close all clients, stop all services, and exit the server
#
#-
# it has four types: string (key,value), list (sequence), set (collection), and zset (ordered collection with one more sequential attribute)
# I don't know which type you use?
# print r.get_type ('a') # can tell you
#
# string operation
Print'-'* 20
# plug data
R ['c1'] =' bar'
# or
R.set ('c2pm, jingle')
# there is a getset attribute here. If it is True, you can create the contents of the last storage at the same time when saving new data.
Print 'getset:',r.getset (' c2mom JJ')
# if you want to set an incrementing integer, it increments by 1 each time it is executed:
Print 'incr:',r.incr (' a')
# if you want to set a decreasing integer please:
Print 'decr:',r.decr (' a')
# fetching data
Print'r [']:', r ['c1']
# or
Print 'get:',r.get (' a')
# or pick up a batch at the same time
Print 'mget:',r.mget (' C1', 'c2')
# or take a batch of them at the same time (key) is very similar and you don't want to lose all of them
Print 'keys:',r.keys (' caterpillar')
# or maybe you just want to take one at random:
Print 'randomkey:',r.randomkey ()
# check whether a data has one or no zero
Print 'existes:',r.exists (' a')
# Delete data 1 is deleted successfully 0 and None is no such thing
Print 'delete:',r.delete (' cc')
# Oh, by the way, it supports batch operations.
Print 'delete:',r.delete (' C1', 'c2')
# other
R.rename ('axiomatica c3') # er. Rename
R.expire ('c3century no. 10) # Let the data expire in 10 seconds. Honestly, I don't quite understand.
R.ttl ('c3') # see the remaining expiration time does not exist. Return-1
#--
# sequence (list) operation
Print'-'* 20
# it's two ends.
# plug in
R.push ('baked last minute')
R.push ('baked last minute hh')
# head attribute controls whether it is plugged from the other end
R.push ('breadth, memory, brain, brain, head, etc.)
# look at the length
Print 'list len:',r.llen (' b')
# list a batch
Print 'list lrange:',r.lrange (' baked Magazine startball 0 Junior endurance Murray 1)
# take out one
Print 'list index 0r.lindex (' baure0)
# trim list
# if start is greater than end, clear the list
Print 'list ltrim:', r.ltrim ('baked dagger, startball 0, endgame 3) # only have four digits from 0 to 3
# sort
# this is a big project
#--
# set operation
# plug data
R.sadd ('slots,' a')
# determine how long a set is and does not exist as 0
R.scard ('s')
# determine whether an object exists in set
R.sismember ('sails, girls and girls')
# find the intersection
R.sadd ('s 2th century, last month')
R.sinter ('s 1','1', 2')
# find the intersection and assign the result
R.sinterstore ('s 3 times, 1's, 2')
# look at a set object
R.smembers ('s3')
# Union set
R.sunion ('s 1','1', 2')
# Ah, I think you've guessed the smart one.
# join and return the result
R.sunionstore ('ss','s1','s2','s3')
# ask for difference
# numbers that are found in S1 but not in S2 and S3
R.sdiff ('s 1, 2, 7, 3, 3, 3, 4, 3, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3')
R.sdiffstore ('s 4 times, 1's, 2') # you know that
# take a random number
R.srandmember ('s1')
#--
# zset ordered set
# 'zadd',' zcard', 'zincr',' zrange', 'zrangebyscore',' zrem', 'zscore'
# corresponding respectively
# add, quantity, add 1, take data, take data according to integral (range), delete, take integral
# God damn it, you're killing me, redis!
# Today in the lab, I tried to insert a piece of zset type data:
R1.zset (ubiquitous www.liyi99.composition.liwuqingphai3)
# inserted successfully
# I'll continue to insert
R1.zset (upright www.liyi99.composition.u'\ u9001\ u793 cantilethal 5)
# error:
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range
# what is inserted this time is the unicode coding of the Chinese word of the gift
# Why did it fail? I took this piece of data from redis and inserted it without any modification.
# the data types returned and accepted by redis are all encoded by unicode
# all right, let's try it again
# insert again
R1.zset ('www.liyi99.com',u'\ u9001\ u793 cantilever 5)
# it worked
# insert
R1.zset ('www.liyi99.com',' Gift', 5)
# still successful, follow the redis.py 1024 line
Return self.send_command ('ZADD% s% s% s\ r\ n% s\ r\ n'% (
Key, score, len (member), member))
# Oh, evil transcoding!
# but when fetching, it doesn't matter what type of data the first one is
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.