Redis實戰之HashMap

前言

在游戲服務器的設計過程中,涉及到用戶游戲數據的存儲和讀取,使用Mysql對其進行操作在一定程度上會增加與數據庫的交互,并且效率太低。在查詢了資料后決定采用Redis中的HashMap對數據進行實時更新,利用定時任務機制將每個10分鐘將Hashmap中的數據同步到數據庫中。首先介紹一下Redis和HashMap:

redis

Redis

Remote Dictionary Server(Redis) 是一個由Salvatore Sanfilippo寫的key-value存儲系統。Redis是一個開源的使用ANSI C語言編寫、遵守BSD協議、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,并提供多種語言的API。它通常被稱為數據結構服務器,因為值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等類型。
HashMap
類似C#中的dict類型或者C++中的hash_map類型。
Redis Hash對應Value內部實際就是一個HashMap,實際這里會有2種不同實現,這個Hash的成員比較少時Redis為了節省內存會采用類似一維數組的方式來緊湊存儲,而不會采用真正的HashMap結構,對應的value redisObject的encoding為zipmap,當成員數量增大時會自動轉成真正的HashMap,此時encoding為ht。

應用場景
假設有多個用戶及對應的用戶信息,可以用來存儲以用戶ID為key,將用戶信息以key-value的形式進行存儲。

相關命令

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

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

  • HGET
    HGET key field 獲取對象中該field屬性域的值

  • HGETALL
    HGETALL key 獲取對象的所有屬性域和值

  • HKEYS
    HKEYS key 獲取對象的所有屬性字段

  • HVALS
    HVALS key 獲取對象的所有屬性值

  • HLEN
    HLEN key 獲取對象的所有屬性字段的總數

  • HMGET
    HMGET key field[field...] 獲取對象的一個或多個指定字段的值

  • HSET
    HSET key field value 設置對象指定字段的值

  • HMSET
    HMSET key field value [field value ...] 同時設置對象中一個或多個字段的值

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

  • HSCAN
    HSCAN key cursor [MATCH pattern] [COUNT count] 類似SCAN命令

使用場景

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實現

<?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保存數據
     * @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 設置某個filed數據
     * @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 獲取某個field數據
     * @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 獲取所有數據
     * @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 刪除數據
     * @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;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容