Redis實(shí)戰(zhàn)之HashMap

前言

在游戲服務(wù)器的設(shè)計(jì)過(guò)程中,涉及到用戶(hù)游戲數(shù)據(jù)的存儲(chǔ)和讀取,使用Mysql對(duì)其進(jìn)行操作在一定程度上會(huì)增加與數(shù)據(jù)庫(kù)的交互,并且效率太低。在查詢(xún)了資料后決定采用Redis中的HashMap對(duì)數(shù)據(jù)進(jìn)行實(shí)時(shí)更新,利用定時(shí)任務(wù)機(jī)制將每個(gè)10分鐘將Hashmap中的數(shù)據(jù)同步到數(shù)據(jù)庫(kù)中。首先介紹一下Redis和HashMap:

redis

Redis

Remote Dictionary Server(Redis) 是一個(gè)由Salvatore Sanfilippo寫(xiě)的key-value存儲(chǔ)系統(tǒng)。Redis是一個(gè)開(kāi)源的使用ANSI C語(yǔ)言編寫(xiě)、遵守BSD協(xié)議、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的API。它通常被稱(chēng)為數(shù)據(jù)結(jié)構(gòu)服務(wù)器,因?yàn)橹担╲alue)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等類(lèi)型。
HashMap
類(lèi)似C#中的dict類(lèi)型或者C++中的hash_map類(lèi)型。
Redis Hash對(duì)應(yīng)Value內(nèi)部實(shí)際就是一個(gè)HashMap,實(shí)際這里會(huì)有2種不同實(shí)現(xiàn),這個(gè)Hash的成員比較少時(shí)Redis為了節(jié)省內(nèi)存會(huì)采用類(lèi)似一維數(shù)組的方式來(lái)緊湊存儲(chǔ),而不會(huì)采用真正的HashMap結(jié)構(gòu),對(duì)應(yīng)的value redisObject的encoding為zipmap,當(dāng)成員數(shù)量增大時(shí)會(huì)自動(dòng)轉(zhuǎn)成真正的HashMap,此時(shí)encoding為ht。

應(yīng)用場(chǎng)景
假設(shè)有多個(gè)用戶(hù)及對(duì)應(yīng)的用戶(hù)信息,可以用來(lái)存儲(chǔ)以用戶(hù)ID為key,將用戶(hù)信息以key-value的形式進(jìn)行存儲(chǔ)。

相關(guān)命令

  • HDEL
    HDEL key field[field...] 刪除對(duì)象的一個(gè)或幾個(gè)屬性域,不存在的屬性將被忽略

  • HEXISTS
    HEXISTS key field 查看對(duì)象是否存在該屬性域

  • HGET
    HGET key field 獲取對(duì)象中該field屬性域的值

  • HGETALL
    HGETALL key 獲取對(duì)象的所有屬性域和值

  • HKEYS
    HKEYS key 獲取對(duì)象的所有屬性字段

  • HVALS
    HVALS key 獲取對(duì)象的所有屬性值

  • HLEN
    HLEN key 獲取對(duì)象的所有屬性字段的總數(shù)

  • HMGET
    HMGET key field[field...] 獲取對(duì)象的一個(gè)或多個(gè)指定字段的值

  • HSET
    HSET key field value 設(shè)置對(duì)象指定字段的值

  • HMSET
    HMSET key field value [field value ...] 同時(shí)設(shè)置對(duì)象中一個(gè)或多個(gè)字段的值

  • HSTRLEN
    HSTRLEN key field 返回對(duì)象指定field的value的字符串長(zhǎng)度,如果該對(duì)象或者field不存在,返回0.

  • HSCAN
    HSCAN key cursor [MATCH pattern] [COUNT count] 類(lèi)似SCAN命令

使用場(chǎng)景

127.0.0.1:6379> hset 101 name jack
(integer) 1
127.0.0.1:6379> hset 101 age 20
(integer) 1
127.0.0.1:6379> hset 101 sex male
(integer) 1
127.0.0.1:6379> hgetall 101
"name"
"jack"
"age"
"20"
"sex"
"male"
127.0.0.1:6379> hget 101 name
"jack"

PHP實(shí)現(xiàn)

<?php

    public $redis;
    public $rankRedis;
    public $rank = 'rank';

    public function __construct()
    {
        $this->redis = new \Redis();
        $redis_host = C("REDIS_HOST");
        $redis_port = C("REDIS_PORT");

        $this->redis->connect($redis_host, $redis_port);
    }


    /**
     * redis 連接
     * @return \Redis
     */
    public function redisConnect()
    {
        $redis = new \Redis();
        $redis_host = C("REDIS_HOST");
        $redis_port = C("REDIS_PORT");

        $redis->connect($redis_host, $redis_port);
        return $redis;
    }

    public function getRedisKeys()
    {
        if ($this->redis == null) {
            throw new \Exception("can not connect redis");
        }

        $ret = $this->redis->keys('*');
        return $ret;
    }

    /**
     * 使用hashset保存數(shù)據(jù)
     * @param $playerid
     * @param $gold
     * @param $gem
     * @param $stamina
     *
     * @throws \Exception
     */
    protected function hsetPlayerRedis($playerid, $gold, $gem, $stamina)
    {
        if ($this->redis == null) {
            throw new \Exception("can not connect redis");
        }

        $this->redis->hSet($playerid, 'gold', $gold);
        $this->redis->hSet($playerid, 'gem', $gem);
        $this->redis->hSet($playerid, 'stamina', $stamina);
    }

    /**
     * hashset 設(shè)置某個(gè)filed數(shù)據(jù)
     * @param $playerid
     * @param $filed
     * @param $value
     *
     * @return int
     * @throws \Exception
     */
    protected function hsetPlayerField($playerid, $filed, $value)
    {
        if ($this->redis == null) {
            throw new \Exception("can not connect redis");
        }

        $ret = $this->redis->hSet($playerid, $filed, $value);
        return $ret;
    }

    /**
     * hashset 獲取某個(gè)field數(shù)據(jù)
     * @param $playerid
     * @param $filed
     *
     * @return string
     * @throws \Exception
     */
    protected function hgetPlayerRedis($playerid, $filed)
    {
        if ($this->redis == null) {
            throw new \Exception("can not connect redis");
        }

        $ret = $this->redis->hGet($playerid, $filed);
        return $ret;
    }

    /**
     * hashset 獲取所有數(shù)據(jù)
     * @param $playerid
     *
     * @return array
     * @throws \Exception
     */
    protected function hgetallPlayerRedis($playerid)
    {
        if ($this->redis == null) {
            throw new \Exception("can not connect redis");
        }

        $ret = $this->redis->hGetAll($playerid);
        return $ret;
    }

    /**
     * hashset 刪除數(shù)據(jù)
     * @param $playerid
     *
     * @throws \Exception
     */
    protected function hdeletePlayerRedis($playerid)
    {
        if ($this->redis == null) {
            throw new \Exception("can not connect redis");
        }

        $ret = $this->redis->delete($playerid);
        return $ret;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容