Redis常用操作有哪些-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!

成都創(chuàng)新互聯(lián)長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為興賓企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站,興賓網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

Redis常用操作有哪些?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

Redis簡介

Redis是完全開源免費(fèi)的高性能Key-Value數(shù)據(jù)庫,有以下幾個(gè)特點(diǎn):

·Redis支持?jǐn)?shù)據(jù)持久化,可以將內(nèi)存中的數(shù)據(jù)保存至磁盤中,重啟可以再次加載進(jìn)行使用。

·Redis不僅僅支持簡單的Key-Value類型的額數(shù)據(jù),同時(shí)還提供list,set,zset(有序集合),hash等數(shù)據(jù)結(jié)構(gòu)的存儲(chǔ)。

·Redis支持?jǐn)?shù)據(jù)的備份,即master-slave模式的數(shù)據(jù)備份。

Redis基本操作

1、字符串相關(guān)操作

Redis常用操作有哪些

2、列表相關(guān)操作

Redis常用操作有哪些

3、集合相關(guān)操作

Redis常用操作有哪些

4、散列(hash)操作

Redis常用操作有哪些

python操作string

import redis
class Test_String(object):
    def __init__(self):
        self.r = redis.StrictRedis(host='localhost', port=6379, db=0)
    def test_set(self):
        """
        設(shè)置一個(gè)值
        :return:
        """
        res = self.r.set('user2','Joshua')
        print(res)
    def test_get(self):
        """
        獲取一個(gè)值
        :return:
        """
        res = self.r.get('user2')
        print(res)
    def test_mset(self):
        """
        設(shè)置多個(gè)鍵值對(duì)
        :return:
        """
        d = {'user3': 'qi', 'user4': 'shuai'}
        res = self.r.mset(d)
        print(res)
    def test_mget(self):
        """
        獲取多個(gè)鍵值對(duì)
        :return:
        """
        d = ['user3', 'user4']
        res = self.r.mget(d)
        print(res)
    def test_del(self):
        """
        刪除一個(gè)鍵值對(duì)
        :return:
        """
        res = self.r.delete('user3')
        print(res)
    def test_incr(self):
        """
        增加1
        :return:
        """
        res = self.r.incr('num')
        print(res)
    def test_decr(self):
        """
        減少1
        :return:
        """
        res = self.r.decr('num')
        print(res)
    def test_append(self):
        """
        添加字符串
        :return:
        """
        res = self.r.append('user3','qi')
        print(res)
def main():
    t = Test_String()
    # t.test_mset()
    # t.test_mget()
    # t.test_del()
    # t.test_set()
    # t.test_get()
    # t.test_incr()
    # t.test_decr()
    t.test_append()
if __name__ == '__main__':
    main()

python 操作列表

import redis
class Test_List(object):
    def __init__(self):
        self.r = redis.StrictRedis(host='localhost', port=6379, db=0)
    def test_push(self):
        l_eat = ['Joshua', 'Amy']
        lres = self.r.lpush('eat2', *l_eat)
        print(lres)
        rres = self.r.rpush('eat2', *l_eat)
        print(rres)
    def test_pop(self):
        res = self.r.lpop('eat2')
        print(res)
        res = self.r.rpop('eat2')
        print(res)
    def test_lindex(self):
        # 獲取某個(gè)偏移量的值
        res = self.r.lindex('eat2',0)
        print(res)
    def test_lrange(self):
        res = self.r.lrange('eat2',0,2)  # 獲取某段偏移量的值
        print(res)
        res = self.r.lrange('eat2',0,-1)  # 獲取列表所有值
        print(res)
    def test_ltrim(self):
        res = self.r.ltrim('eat2', 1,2)  # 竊取一段值,其他值刪除掉
        print(res)
        res = self.r.lrange('eat2', 0, -1)
        print(res)
    def test_bpop(self):
        res = self.r.blpop('eat2',timeout=3)  # 在3秒內(nèi)從列表左端刪除一個(gè)元素
        print(res)
        res = self.r.brpop('eat2',timeout=3)  # 在3秒內(nèi)從列表右端刪除一個(gè)元素
        print(res)
    def test_rpoplpush(self):
        res = self.r.rpoplpush('mylist', 'youlist')  # 從mylist的右端刪除一個(gè)元素,添加到y(tǒng)oulist的最左邊
        print(res)
    def test_brpoplpush(self):
        # 從mylist的右端刪除一個(gè)元素,添加到y(tǒng)oulist的最左邊,如果mylist為空則等待3秒
        res = self.r.brpoplpush('mylist', 'youlist',timeout=3)
        print(res)
    def test_pushx(self):
        # 當(dāng)key存在的時(shí)候才往列表左端插入一個(gè)數(shù)據(jù)
        res = self.r.lpushx('youlist', 1)
        print(res)
        # ~右端
        res = self.r.rpushx('itslist',1)
        print(res)
if __name__ == '__main__':
    t = Test_List()
    # t.test_push()
    # t.test_pop()
    # t.test_lindex()
    # t.test_lrange()
    # t.test_ltrim()
    # t.test_blpop()
    # t.test_rpoplpush()
    # t.test_brpoplpush()
    t.test_pushx()

python操作集合

import redis
class Test_Set(object):
    def __init__(self):
        self.r = redis.StrictRedis(host='localhost',port=6379,db=0)
    def test_sadd(self):
        data = ['cat', 'dog']
        res = self.r.sadd('zoo1', *data)
        print(res)
        res = self.r.smembers('zoo1')  # 獲得集合的所有元素
        print(res)
    def test_srem(self):
        # data = ['cat', 'dog']
        # res = self.r.srem('zoo', *data)  # 刪除多個(gè)元素
        res = self.r.srem('zoo','dog')  # 刪除單個(gè)元素
        print(res)
        res = self.r.smembers('zoo')
        print(res)
    def test_sinter(self):  # 獲取兩個(gè)集合的交集
        res = self.r.sinter('zoo','zoo1')
        print(res)
    def test_sunion(self):  # 獲取兩個(gè)集合的并集
        res = self.r.sunion('zoo','zoo1')
        print(res)
    def test_sdiff(self):  # 獲取兩個(gè)集合不同之處
        res = self.r.sdiff('zoo','zoo1')
        print(res)
if __name__ == '__main__':
    t = Test_Set()
    # t.test_sadd()
    # t.test_srem()
    # t.test_sinter()
    # t.test_sunion()
    t.test_sdiff()

python操作散列

import redis
class Test_Hash(object):
    def __init__(self):
        self.r = redis.StrictRedis(host='localhost', port=6379, db=0)
    def test_hset(self):  # 設(shè)置一個(gè)哈希值
        res = self.r.hset('News:1', 'Title', 'News Title')
        print(res)
    def test_hdel(self):  # 刪除一個(gè)哈希值
        res = self.r.hdel('News:1', 'Title')
        print(res)
    def test_hget(self):  # 獲取一個(gè)值
        res = self.r.hget('News:1', 'Title')
        print(res)
    def test_heists(self):  # 判斷是否存在
        res = self.r.hexists('News:1', 'Title')
        print(res)
    def test_hgetall(self):  # 獲取所有哈希
        res = self.r.hgetall('News:1')
        print(res)
    def test_hmset(self):  # 設(shè)置多個(gè)哈希
        data = {'content':'this is content', 'data':'20190101'}
        res = self.r.hmset('News:1', data)
        print(res)
    def test_hmget(self):  # 獲取多個(gè)哈希
        fields = ['content', 'data']
        res = self.r.hmget('News:1',fields)
        print(res)
    def test_hkeys(self):  # 獲取所有keys
        res = self.r.hkeys('News:1')
        print(res)
    def test_hvalues(self):  # 獲取所有values
        res = self.r.hvals('News:1')
        print(res)
    def test_hlen(self):  # 獲取fields的數(shù)量
        res = self.r.hlen('News:1')
        print(res)
    def test_hsetnx(self):  # 設(shè)置一個(gè)哈希值,如果存在則不設(shè)置
        res = self.r.hsetnx('News:1', 'content', 'fuck')
        print(res)
if __name__ == '__main__':
    t = Test_Hash()
    # t.test_hset()
    # t.test_hdel()
    # t.test_hget()
    # t.test_heists()
    # t.test_hgetall()
    # t.test_hmset()
    # t.test_hmget()
    # t.test_hkeys()
    # t.test_hvalues()
    # t.test_hlen()
    t.test_hsetnx()

關(guān)于Redis常用操作有哪些問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

分享名稱:Redis常用操作有哪些-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://bm7419.com/article22/ceogjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、建站公司、動(dòng)態(tài)網(wǎng)站、靜態(tài)網(wǎng)站、定制網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化