Redis&sqlalchemy

Source: Internet
Author: User

Redis is a key-value storage system it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection sets and differences and richer operations, and these operations are atomic

Some uses of Redis

# See information
info = R.info ()
For key in info:

Print "%s:%s"% (key, Info[key])

# Check Database size
print ' \ndbsize:%s '% r.dbsize ()

# See Connections
Print "Ping%s"% r.ping ()

# Select Database
#r. Select (2)

# move data to 2 database
#r. Move (' a ', 2)

# Other
#r. Save (' a ') # Save data
#r. Lastsave (' a ') # take the Last save time
#r. Flush () #刷新
#r. Shutdown () #关闭所有客户端, shut down all services, exit the server
#
#--------------------------------------------
# It has four types: string (key,val), list (sequence), set (set), Zset (ordered set, one more order attribute)
# Don't know what kind you're using?
# Print R.get_type (' a ') #可以告诉你
# -------------------------------------------
# string manipulation
print '-' *20
# Plug Data
r[' C1 '] = ' bar '
#或者
R.set (' C2 ', ' Bar ')
#这里有个 the Getset property, if TR can make the last stored content while saving new data
print ' Getset: ', R.getset (' C2 ', ' JJ ')
#如果你想设置一个递增的整数 every time it is executed it adds 1:
print ' incr: ', R.INCR (' a ')
#如果你想设置一个递减的整数 please:
print ' DECR: ', R.DECR (' a ')

# Fetch Data
print ' r[': ', r[' C1 ']
#或者
print ' Get: ', R.get (' a ')
#或者 take a batch at the same time
print ' Mget: ', R.mget (' C1 ', ' C2 ')
#或者 take a batch of their names (key) very much like and you don't want to lose all
print ' keys: ', R.keys (' c* ')
#又或者 you just want to take one randomly:
print ' Randomkey: ', R.randomkey ()

# View a data there's No 1 no 0
print ' Existes: ', r.exists (' a ')

# Delete Data 1 is deleted success 0 and none is not this thing
print ' Delete: ', R.delete (' CC ')
# Oh, yes. It is supported for bulk operations
print ' Delete: ', R.delete (' C1 ', ' C2 ')

# Other
R.rename (' A ', ' C3 ') #呃. renaming
R.expire (' C3 ', ten) #让数据10秒后过期 Honestly, I don't know what that means.
R.ttl (' C3 ') #看剩余过期时间 not present return-1

#--------------------------------
# sequence (list) operation
print '-' *20
# It's two-way through #
# Plug In
R.push (' B ', ' GG ')
R.push (' B ', ' hh ')
# Head Property control is not from the other end of the plug
R.push (' B ', ' EE ')
# Look at the length
print ' list len: ', R.llen (' B ')
# List A batch out
print ' list lrange: ', R.lrange (' B ', start=0,end=-1)

# Take out one
print ' list index 0: ', r.lindex (' B ', 0)

# Trim List
#若start is greater than end, the list is emptied
print ' list LTrim: ', R.ltrim (' B ', start=0,end=3) #只留 from 0 to 34 bits

# sort
# It's a big project #

#--------------------------------
# collection (SET) operation
# Plug Data
R.sadd (' s ', ' a ')

# Determine how much a set length is not present as 0
R.scard (' s ')

# Determine if an object exists in set
R.sismember (' s ', ' a ')

# Ask for the intersection
R.sadd (' S2 ', ' a ')
R.sinter (' s1 ', ' s2 ')

#求交集并将结果赋值
R.sinterstore (' S3 ', ' s1 ', ' s2 ')

# Look at a set object
R.smembers (' S3 ')

# Seek and set
R.sunion (' s1 ', ' s2 ')
# I think it's smart, you've guessed it #
#求并集 and returns the result
R.sunionstore (' ss ', ' s1 ', ' s2 ', ' S3 ')

# Ask for a different
# in S1, but not in S2 and S3
R.sdiff (' s1 ', ' s2 ', ' S3 ')
R.sdiffstore (' S4 ', ' s1 ', ' S2 ') # This you know

# Take a random number
R.srandmember (' s1 ')

#-------------------------------------
#zset ordered set
# ' Zadd ', ' zcard ', ' zincr ', ' zrange ', ' zrangebyscore ', ' zrem ', ' Zscore '
# correspond to each other
#添加, quantity, add 1, Fetch data, collect data according to points (range), delete, collect points

Basic query for SQLAlchemy

#获取所有数据
Session.query (person). All ()

#获取某一列数据, similar to the Django get, if the return data is multiple then an error
Session.query (person). Filter (person.name==' Test '). One ()

#获取返回数据的第一行
Session.query (person). First ()

#过滤数据
Session.query (Person.name). Filter (Person.id>1). All ()

#limit
Session.query (person). All () [1:3]

#order by
Session.query (person). Ordre_by (-Person.id)

#equal/like/in
query = Session.query (person)
Query.filter (person.id==1). All ()
Query.filter (person.id!=1). All ()
Query.filter (Person.name.like ('%ac% '). All ()
Query.filter (Person.id.Inch_([1,2,3]). All ()
Query.filter (~Person.id.Inch_([1,2,3]). All ()
Query.filter (person.name==None). All ()

#and or
From SQLAlchemy Importand_
Query.filter (and _ (person.id==1, person.name==' Jack ')). All ()
Query.filter (person.id==1, person.name==' Jack '). All ()
Query.filter (person.id==1). Filter (person.name==' Jack '). All ()
From SQLAlchemy import or_
Query.filter (or_ (person.id==1, person.id==2)). All ()

Redis&sqlalchemy

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.