forked from huangzworks/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrandmember.html
229 lines (190 loc) · 8.81 KB
/
srandmember.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SRANDMEMBER — Redis 命令参考</title>
<link rel="stylesheet" href="../_static/pyramid.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.8',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="Redis 命令参考" href="../index.html" />
<link rel="up" title="Set(集合)" href="index.html" />
<link rel="next" title="SREM" href="srem.html" />
<link rel="prev" title="SPOP" href="spop.html" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="../_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="srem.html" title="SREM"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="spop.html" title="SPOP"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">Redis 命令参考</a> »</li>
<li><a href="index.html" accesskey="U">Set(集合)</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="srandmember">
<span id="id1"></span><h1>SRANDMEMBER<a class="headerlink" href="#srandmember" title="Permalink to this headline">¶</a></h1>
<p><strong>SRANDMEMBER key [count]</strong></p>
<p>如果命令执行时,只提供了 <code class="docutils literal"><span class="pre">key</span></code> 参数,那么返回集合中的一个随机元素。</p>
<p>从 Redis 2.6 版本开始, <a class="reference internal" href="#srandmember">SRANDMEMBER</a> 命令接受可选的 <code class="docutils literal"><span class="pre">count</span></code> 参数:</p>
<ul class="simple">
<li>如果 <code class="docutils literal"><span class="pre">count</span></code> 为正数,且小于集合基数,那么命令返回一个包含 <code class="docutils literal"><span class="pre">count</span></code> 个元素的数组,数组中的元素<strong>各不相同</strong>。如果 <code class="docutils literal"><span class="pre">count</span></code> 大于等于集合基数,那么返回整个集合。</li>
<li>如果 <code class="docutils literal"><span class="pre">count</span></code> 为负数,那么命令返回一个数组,数组中的元素<strong>可能会重复出现多次</strong>,而数组的长度为 <code class="docutils literal"><span class="pre">count</span></code> 的绝对值。</li>
</ul>
<p>该操作和 <a class="reference internal" href="spop.html#spop"><span>SPOP</span></a> 相似,但 <a class="reference internal" href="spop.html#spop"><span>SPOP</span></a> 将随机元素从集合中移除并返回,而 <a class="reference internal" href="#srandmember">SRANDMEMBER</a> 则仅仅返回随机元素,而不对集合进行任何改动。</p>
<dl class="docutils">
<dt><strong>可用版本:</strong></dt>
<dd>>= 1.0.0</dd>
<dt><strong>时间复杂度:</strong></dt>
<dd><div class="first last line-block">
<div class="line">只提供 <code class="docutils literal"><span class="pre">key</span></code> 参数时为 O(1) 。</div>
<div class="line">如果提供了 <code class="docutils literal"><span class="pre">count</span></code> 参数,那么为 O(N) ,N 为返回数组的元素个数。</div>
</div>
</dd>
<dt><strong>返回值:</strong></dt>
<dd><div class="first last line-block">
<div class="line">只提供 <code class="docutils literal"><span class="pre">key</span></code> 参数时,返回一个元素;如果集合为空,返回 <code class="docutils literal"><span class="pre">nil</span></code> 。</div>
<div class="line">如果提供了 <code class="docutils literal"><span class="pre">count</span></code> 参数,那么返回一个数组;如果集合为空,返回空数组。</div>
</div>
</dd>
</dl>
<div class="highlight-python"><div class="highlight"><pre># 添加元素
redis> SADD fruit apple banana cherry
(integer) 3
# 只给定 key 参数,返回一个随机元素
redis> SRANDMEMBER fruit
"cherry"
redis> SRANDMEMBER fruit
"apple"
# 给定 3 为 count 参数,返回 3 个随机元素
# 每个随机元素都不相同
redis> SRANDMEMBER fruit 3
1) "apple"
2) "banana"
3) "cherry"
# 给定 -3 为 count 参数,返回 3 个随机元素
# 元素可能会重复出现多次
redis> SRANDMEMBER fruit -3
1) "banana"
2) "cherry"
3) "apple"
redis> SRANDMEMBER fruit -3
1) "apple"
2) "apple"
3) "cherry"
# 如果 count 是整数,且大于等于集合基数,那么返回整个集合
redis> SRANDMEMBER fruit 10
1) "apple"
2) "banana"
3) "cherry"
# 如果 count 是负数,且 count 的绝对值大于集合的基数
# 那么返回的数组的长度为 count 的绝对值
redis> SRANDMEMBER fruit -10
1) "banana"
2) "apple"
3) "banana"
4) "cherry"
5) "apple"
6) "apple"
7) "cherry"
8) "apple"
9) "apple"
10) "banana"
# SRANDMEMBER 并不会修改集合内容
redis> SMEMBERS fruit
1) "apple"
2) "cherry"
3) "banana"
# 集合为空时返回 nil 或者空数组
redis> SRANDMEMBER not-exists
(nil)
redis> SRANDMEMBER not-eixsts 10
(empty list or set)
</pre></div>
</div>
</div>
<div class="section" id="discuss">
<h2>
讨论
<a class="headerlink" href="#discuss" title="永久链接至标题">¶</a>
</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'redis-command-cn'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
<div id="sponser">
<h2>赞助商</h2>
<a href="http://yunba.io/"><img src="../_static/yunba-logo.jpg"/></a>
<p><a href="http://yunba.io/">云巴(yunba.io)</a>,专业的App.Web.物联网实时通信后端云服务提供商。</p>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="srem.html" title="SREM"
>next</a> |</li>
<li class="right" >
<a href="spop.html" title="SPOP"
>previous</a> |</li>
<li><a href="../index.html">Redis 命令参考</a> »</li>
<li><a href="index.html" >Set(集合)</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2015, Redis.
Last updated on Dec 02, 2015.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.1.
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-53959484-7', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>