This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 861
Expand file tree
/
Copy pathRedisClientSet.cs
More file actions
173 lines (147 loc) · 3.84 KB
/
RedisClientSet.cs
File metadata and controls
173 lines (147 loc) · 3.84 KB
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
//
// https://github.com/mythz/ServiceStack.Redis
// ServiceStack.Redis: ECMA CLI Binding to the Redis key-value storage system
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2013 ServiceStack.
//
// Licensed under the same terms of Redis and ServiceStack: new BSD license.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ServiceStack.Redis
{
/// <summary>
/// Wrap the common redis set operations under a ICollection[string] interface.
/// </summary>
internal class RedisClientSet
: IRedisSet
{
private readonly RedisClient client;
private readonly string setId;
private const int PageLimit = 1000;
public RedisClientSet(RedisClient client, string setId)
{
this.client = client;
this.setId = setId;
}
public IEnumerator<string> GetEnumerator()
{
return this.Count <= PageLimit
? client.GetAllItemsFromSet(setId).GetEnumerator()
: GetPagingEnumerator();
}
public IEnumerator<string> GetPagingEnumerator()
{
var skip = 0;
List<string> pageResults;
do
{
pageResults = client.GetSortedEntryValues(setId, skip, skip + PageLimit - 1);
foreach (var result in pageResults)
{
yield return result;
}
skip += PageLimit;
} while (pageResults.Count == PageLimit);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(string item)
{
client.AddItemToSet(setId, item);
}
public void Clear()
{
client.Remove(setId);
}
public bool Contains(string item)
{
return client.SetContainsItem(setId, item);
}
public void CopyTo(string[] array, int arrayIndex)
{
var allItemsInSet = client.GetAllItemsFromSet(setId);
allItemsInSet.CopyTo(array, arrayIndex);
}
public bool Remove(string item)
{
client.RemoveItemFromSet(setId, item);
return true;
}
public int Count
{
get
{
return (int)client.GetSetCount(setId);
}
}
public bool IsReadOnly { get { return false; } }
public string Id
{
get { return this.setId; }
}
public List<string> GetRangeFromSortedSet(int startingFrom, int endingAt)
{
return client.GetSortedEntryValues(setId, startingFrom, endingAt);
}
public HashSet<string> GetAll()
{
return client.GetAllItemsFromSet(setId);
}
public string Pop()
{
return client.PopItemFromSet(setId);
}
public void Move(string value, IRedisSet toSet)
{
client.MoveBetweenSets(setId, toSet.Id, value);
}
private List<string> MergeSetIds(IRedisSet[] withSets)
{
var allSetIds = new List<string> { setId };
allSetIds.AddRange(withSets.ToList().ConvertAll(x => x.Id));
return allSetIds;
}
public HashSet<string> Intersect(params IRedisSet[] withSets)
{
var allSetIds = MergeSetIds(withSets);
return client.GetIntersectFromSets(allSetIds.ToArray());
}
public void StoreIntersect(params IRedisSet[] withSets)
{
var withSetIds = withSets.ToList().ConvertAll(x => x.Id).ToArray();
client.StoreIntersectFromSets(setId, withSetIds);
}
public HashSet<string> Union(params IRedisSet[] withSets)
{
var allSetIds = MergeSetIds(withSets);
return client.GetUnionFromSets(allSetIds.ToArray());
}
public void StoreUnion(params IRedisSet[] withSets)
{
var withSetIds = withSets.ToList().ConvertAll(x => x.Id).ToArray();
client.StoreUnionFromSets(setId, withSetIds);
}
public HashSet<string> Diff(IRedisSet[] withSets)
{
var withSetIds = withSets.ToList().ConvertAll(x => x.Id).ToArray();
return client.GetDifferencesFromSet(setId, withSetIds);
}
public void StoreDiff(IRedisSet fromSet, params IRedisSet[] withSets)
{
var withSetIds = withSets.ToList().ConvertAll(x => x.Id).ToArray();
client.StoreDifferencesFromSet(setId, fromSet.Id, withSetIds);
}
public string GetRandomEntry()
{
return client.GetRandomItemFromSet(setId);
}
}
}