Redis实战之HashMap

前言

在游戏服务器的设计过程中,涉及到用户游戏数据的存储和读取,使用Mysql对其进行操作在一定程度上会增加与数据库的交互,并且效率太低。在查询了资料后决定采用Redis中的HashMap对数据进行实时更新,利用定时任务机制将每个10分钟将Hashmap中的数据同步到数据库中。首先介绍一下Redis和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;
    }
}
    原文作者:小白frankie
    原文地址: https://www.jianshu.com/p/490e4b7fed13
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞