Home  >  Article  >  Backend Development  >  Record user gender using redis bit operations

Record user gender using redis bit operations

WBOY
WBOYOriginal
2016-12-01 00:25:481596browse

Reading and seeing content related to redis bit operations, there is an example in the book: For example, each user on the website has an auto-incremented ID, and a string type key is used to record the user's gender..., I want to know the specific operation of this recording method, or what the idea is. I know that it must be distinguished by bits 1 and 0, but how is it recorded?

Reply content:

Reading and seeing content related to redis bit operations, there is an example in the book: For example, each user on the website has an auto-incremented ID, and a string type key is used to record the user's gender..., I want to know the specific operation of this recording method, or what the idea is. I know that it must be distinguished by bits 1 and 0, but how is it recorded?

setbit key offset, where offset is replaced by userid, if the user with ID 1 is male, just setbit key 1 1, if it is female, just setbit key 1 0, to get the gender of ID 1, just getbit key 1

<code class="php">$redis->mSet($userId,['age'=>18, 'gender'=>0]);//小明
$redis->mSet($userId2,['age'=>16, 'gender'=>1]);//小红

$user = $redis->mGet($userId);
echo $user['gender'];//
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn