Skip to content

Commit 257c3b9

Browse files
committed
#### Version 1.4.9.1
* 内置Vendor目录仅保留 golang.org/x/net包,移除redis与yaml包 * 完善RedisClient接口能力 * 2018-03-13 12:00
1 parent abebf57 commit 257c3b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+244
-17371
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ type NotFoundHandle func(http.ResponseWriter, *http.Request)
269269
```
270270

271271
## Dependency
272-
websocket - golang.org/x/net/websocket
272+
websocket - golang.org/x/net/websocket - 内置vendor
273273
<br>
274-
redis - github.com/garyburd/redigo/redis
274+
redis - github.com/garyburd/redigo - 需自行go get
275275
<br>
276-
yaml - gopkg.in/yaml.v2
276+
yaml - gopkg.in/yaml.v2 - 需自行go get
277277

278278
## 相关项目
279279
#### <a href="https://github.com/devfeel/longweb" target="_blank">LongWeb</a>

framework/redis/redisutil.go

Lines changed: 236 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ func (rc *RedisClient) SetNX(key, value string) (interface{}, error){
173173
}
174174

175175

176-
// HGet 获取指定hashset的内容
176+
177+
//****************** hash 集合 ***********************
178+
179+
// HGet 获取指定hash的内容
177180
func (rc *RedisClient) HGet(hashID string, field string) (string, error) {
178181
conn := rc.pool.Get()
179182
defer conn.Close()
@@ -185,37 +188,53 @@ func (rc *RedisClient) HGet(hashID string, field string) (string, error) {
185188
return val, err
186189
}
187190

188-
// HGetAll 获取指定hashset的所有内容
191+
// HGetAll 获取指定hash的所有内容
189192
func (rc *RedisClient) HGetAll(hashID string) (map[string]string, error) {
190193
conn := rc.pool.Get()
191194
defer conn.Close()
192195
reply, err := redis.StringMap(conn.Do("HGetAll", hashID))
193196
return reply, err
194197
}
195198

196-
// HSet 设置指定hashset的内容
199+
// HSet 设置指定hash的内容
197200
func (rc *RedisClient) HSet(hashID string, field string, val string) error {
198201
conn := rc.pool.Get()
199202
defer conn.Close()
200203
_, err := conn.Do("HSET", hashID, field, val)
201204
return err
202205
}
203206

204-
// HSetNX 设置指定hashset的内容, 如果field不存在, 该操作无效
205-
func (rc *RedisClient) HSetNX(key, field, value string) (interface{}, error) {
207+
// HSetNX 设置指定hash的内容, 如果field不存在, 该操作无效
208+
func (rc *RedisClient) HSetNX(hashID, field, value string) (interface{}, error) {
206209
conn := rc.pool.Get()
207210
defer conn.Close()
208211

209-
val, err := conn.Do("HSETNX", key, field, value)
212+
val, err := conn.Do("HSETNX", hashID, field, value)
213+
return val, err
214+
}
215+
216+
// HExist 返回hash里面field是否存在
217+
func (rc *RedisClient) HExist(hashID string, field string) (int, error){
218+
conn := rc.pool.Get()
219+
defer conn.Close()
220+
val, err := redis.Int(conn.Do("HEXISTS", hashID, field))
221+
return val, err
222+
}
223+
224+
// HIncrBy 增加 key 指定的哈希集中指定字段的数值
225+
func (rc *RedisClient) HIncrBy(hashID string, field string, increment int)(int, error){
226+
conn := rc.pool.Get()
227+
defer conn.Close()
228+
val, err := redis.Int(conn.Do("HINCRBY", hashID, field, increment))
210229
return val, err
211230
}
212231

213232
// HLen 返回哈希表 key 中域的数量, 当 key 不存在时,返回0
214-
func (rc *RedisClient) HLen(key string) (int64, error) {
233+
func (rc *RedisClient) HLen(hashID string) (int64, error) {
215234
conn := rc.pool.Get()
216235
defer conn.Close()
217236

218-
val, err := redis.Int64(conn.Do("HLEN", key))
237+
val, err := redis.Int64(conn.Do("HLEN", hashID))
219238
return val, err
220239
}
221240

@@ -229,92 +248,274 @@ func (rc *RedisClient) HDel(args ...interface{}) (int64, error) {
229248
}
230249

231250
// HVals 返回哈希表 key 中所有域的值, 当 key 不存在时,返回空
232-
func (rc *RedisClient) HVals(key string) (interface{}, error) {
251+
func (rc *RedisClient) HVals(hashID string) (interface{}, error) {
233252
conn := rc.pool.Get()
234253
defer conn.Close()
235254

236-
val, err := redis.Strings(conn.Do("HVALS", key))
255+
val, err := redis.Strings(conn.Do("HVALS", hashID))
237256
return val, err
238257
}
239258

259+
//****************** list ***********************
240260

241-
// BRPop 删除,并获得该列表中的最后一个元素,或阻塞,直到有一个可用
242-
func (rc *RedisClient) BRPop(key string) (string, error) {
261+
//将所有指定的值插入到存于 key 的列表的头部
262+
func (rc *RedisClient) LPush(key string, value ...interface{}) (int, error) {
243263
conn := rc.pool.Get()
244264
defer conn.Close()
245-
val, err := redis.StringMap(conn.Do("BRPOP", key, defaultTimeout))
265+
ret, err := redis.Int(conn.Do("LPUSH", key, value))
246266
if err != nil {
247-
return "", err
267+
return -1, err
248268
} else {
249-
return val[key], nil
269+
return ret, nil
250270
}
251271
}
252272

253-
//LPush 将所有指定的值插入到存于 key 的列表的头部
254-
func (rc *RedisClient) LPush(key string, val string) (int64, error) {
273+
func (rc *RedisClient) LPushX(key string, value string) (int, error) {
255274
conn := rc.pool.Get()
256275
defer conn.Close()
257-
ret, err := redis.Int64(conn.Do("LPUSH", key, val))
258-
if err != nil {
259-
return -1, err
260-
} else {
261-
return ret, nil
262-
}
276+
resp, err := redis.Int(conn.Do("LPUSHX", key, value))
277+
return resp, err
278+
}
279+
280+
func (rc *RedisClient) LRange(key string, start int, stop int) ([]string, error){
281+
conn := rc.pool.Get()
282+
defer conn.Close()
283+
resp, err := redis.Strings(conn.Do("LRANGE", key, start, stop))
284+
return resp, err
285+
}
286+
287+
func (rc *RedisClient) LRem(key string, count int, value string) (int, error){
288+
conn := rc.pool.Get()
289+
defer conn.Close()
290+
resp, err := redis.Int(conn.Do("LREM", key, count, value))
291+
return resp, err
292+
}
293+
294+
func (rc *RedisClient) LSet(key string, index int, value string)(string, error){
295+
conn := rc.pool.Get()
296+
defer conn.Close()
297+
resp, err := redis.String(conn.Do("LSET", key, index, value))
298+
return resp, err
299+
}
300+
301+
func (rc *RedisClient) LTrim(key string, start int, stop int) (string, error) {
302+
conn := rc.pool.Get()
303+
defer conn.Close()
304+
resp, err := redis.String(conn.Do("LTRIM", key, start, stop))
305+
return resp, err
306+
}
307+
308+
func (rc *RedisClient) RPop(key string) (string, error) {
309+
conn := rc.pool.Get()
310+
defer conn.Close()
311+
resp, err := redis.String(conn.Do("RPOP", key))
312+
return resp, err
313+
}
314+
315+
func (rc *RedisClient) RPush(key string, value ...interface{}) (int, error){
316+
conn := rc.pool.Get()
317+
defer conn.Close()
318+
args := append([]interface{}{key}, value...)
319+
resp, err := redis.Int(conn.Do("RPUSH", args...))
320+
return resp, err
321+
}
322+
323+
func (rc *RedisClient) RPushX(key string, value ...interface{}) (int, error) {
324+
conn := rc.pool.Get()
325+
defer conn.Close()
326+
args := append([]interface{}{key}, value...)
327+
resp, err := redis.Int(conn.Do("RPUSHX", args...))
328+
return resp, err
329+
}
330+
331+
func (rc *RedisClient) RPopLPush(source string, destination string)(string, error) {
332+
conn := rc.pool.Get()
333+
defer conn.Close()
334+
resp, err := redis.String(conn.Do("RPOPLPUSH", source, destination))
335+
return resp, err
336+
}
337+
338+
339+
func (rc *RedisClient) BLPop(key ...interface{})(map[string]string, error){
340+
conn := rc.pool.Get()
341+
defer conn.Close()
342+
val, err := redis.StringMap(conn.Do("BLPOP", key, defaultTimeout))
343+
return val, err
344+
}
345+
346+
//删除,并获得该列表中的最后一个元素,或阻塞,直到有一个可用
347+
func (rc *RedisClient) BRPop(key ...interface{}) (map[string]string, error) {
348+
conn := rc.pool.Get()
349+
defer conn.Close()
350+
val, err := redis.StringMap(conn.Do("BRPOP", key, defaultTimeout))
351+
return val, err
352+
}
353+
354+
func (rc *RedisClient) BRPopLPush(source string, destination string)(string, error){
355+
conn := rc.pool.Get()
356+
defer conn.Close()
357+
val, err := redis.String(conn.Do("BRPOPLPUSH", source, destination))
358+
return val, err
359+
}
360+
361+
func (rc *RedisClient) LIndex(key string, index int)(string, error){
362+
conn := rc.pool.Get()
363+
defer conn.Close()
364+
val, err := redis.String(conn.Do("LINDEX", key, index))
365+
return val, err
366+
}
367+
368+
func (rc *RedisClient) LInsertBefore(key string, pivot string, value string)(int, error){
369+
conn := rc.pool.Get()
370+
defer conn.Close()
371+
val, err := redis.Int(conn.Do("LINSERT", key, "BEFORE", pivot, value))
372+
return val, err
373+
}
374+
375+
func (rc *RedisClient) LInsertAfter(key string, pivot string, value string)(int, error){
376+
conn := rc.pool.Get()
377+
defer conn.Close()
378+
val, err := redis.Int(conn.Do("LINSERT", key, "AFTER", pivot, value))
379+
return val, err
380+
}
381+
382+
func (rc *RedisClient) LLen(key string)(int, error){
383+
conn := rc.pool.Get()
384+
defer conn.Close()
385+
val, err := redis.Int(conn.Do("LLEN", key))
386+
return val, err
387+
}
388+
389+
func (rc *RedisClient) LPop(key string)(string, error){
390+
conn := rc.pool.Get()
391+
defer conn.Close()
392+
val, err := redis.String(conn.Do("LPOP", key))
393+
return val, err
263394
}
264395

396+
//****************** set 集合 ***********************
397+
265398
// SAdd 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。
266399
// 假如 key 不存在,则创建一个只包含 member 元素作成员的集合。
267-
func (rc *RedisClient) SAdd(args ...interface{}) (int64, error){
400+
func (rc *RedisClient) SAdd(key string, member ...interface{}) (int, error){
268401
conn := rc.pool.Get()
269402
defer conn.Close()
270-
271-
val, err := redis.Int64(conn.Do("SADD", args...))
403+
args := append([]interface{}{key}, member...)
404+
val, err := redis.Int(conn.Do("SADD", args...))
272405
return val, err
273406
}
274407

275408
// SCard 返回集合 key 的基数(集合中元素的数量)。
276409
// 返回值:
277410
// 集合的基数。
278411
// 当 key 不存在时,返回 0
279-
func (rc *RedisClient) SCard(key string) (int64, error) {
412+
func (rc *RedisClient) SCard(key string) (int, error) {
280413
conn := rc.pool.Get()
281414
defer conn.Close()
282-
283-
val, err := redis.Int64(conn.Do("SCARD", key))
415+
val, err := redis.Int(conn.Do("SCARD", key))
284416
return val, err
285417
}
286418

287419
// SPop 移除并返回集合中的一个随机元素。
288420
// 如果只想获取一个随机元素,但不想该元素从集合中被移除的话,可以使用 SRANDMEMBER 命令。
421+
// count 为 返回的随机元素的数量
289422
func (rc *RedisClient) SPop(key string) (string, error) {
290423
conn := rc.pool.Get()
291424
defer conn.Close()
292-
293425
val, err := redis.String(conn.Do("SPOP", key))
294426
return val, err
295427
}
296428

297429
// SRandMember 如果命令执行时,只提供了 key 参数,那么返回集合中的一个随机元素。
298430
// 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 SRANDMEMBER 则仅仅返回随机元素,而不对集合进行任何改动。
299-
func (rc *RedisClient) SRandMember(args ...interface{}) (string, error) {
431+
// count 为 返回的随机元素的数量
432+
func (rc *RedisClient) SRandMember(key string, count int) ([]string, error) {
300433
conn := rc.pool.Get()
301434
defer conn.Close()
302-
303-
val, err := redis.String(conn.Do("SRANDMEMBER", args...))
435+
val, err := redis.Strings(conn.Do("SRANDMEMBER", key, count))
304436
return val, err
305437
}
306438

307439
// SRem 移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。
308440
// 当 key 不是集合类型,返回一个错误。
309441
// 在 Redis 2.4 版本以前, SREM 只接受单个 member 值。
310-
func (rc *RedisClient) SRem(args ...interface{}) (string, error) {
442+
func (rc *RedisClient) SRem(key string, member ...interface{}) (int, error) {
443+
conn := rc.pool.Get()
444+
defer conn.Close()
445+
args := append([]interface{}{key}, member...)
446+
val, err := redis.Int(conn.Do("SREM", args...))
447+
return val, err
448+
}
449+
450+
func (rc *RedisClient) SDiff(key ...interface{}) ([]string, error){
451+
conn := rc.pool.Get()
452+
defer conn.Close()
453+
val, err := redis.Strings(conn.Do("SDIFF", key...))
454+
return val, err
455+
}
456+
457+
func (rc *RedisClient) SDiffStore(destination string, key ...interface{}) (int, error){
458+
conn := rc.pool.Get()
459+
defer conn.Close()
460+
args := append([]interface{}{destination}, key...)
461+
val, err := redis.Int(conn.Do("SDIFFSTORE", args...))
462+
return val, err
463+
}
464+
465+
func (rc *RedisClient) SInter(key ...interface{}) ([]string, error){
466+
conn := rc.pool.Get()
467+
defer conn.Close()
468+
val, err := redis.Strings(conn.Do("SINTER", key...))
469+
return val, err
470+
}
471+
472+
func (rc *RedisClient) SInterStore(destination string, key ...interface{})(int, error){
473+
conn := rc.pool.Get()
474+
defer conn.Close()
475+
args := append([]interface{}{destination}, key...)
476+
val, err := redis.Int(conn.Do("SINTERSTORE", args...))
477+
return val, err
478+
}
479+
480+
func (rc *RedisClient) SIsMember(key string, member string) (bool, error){
481+
conn := rc.pool.Get()
482+
defer conn.Close()
483+
val, err := redis.Bool(conn.Do("SISMEMBER", key, member))
484+
return val, err
485+
}
486+
487+
func (rc *RedisClient) SMembers(key string) ([]string, error){
488+
conn := rc.pool.Get()
489+
defer conn.Close()
490+
val, err := redis.Strings(conn.Do("SMEMBERS", key))
491+
return val, err
492+
}
493+
494+
// smove is a atomic operate
495+
func (rc *RedisClient) SMove(source string, destination string, member string) (bool, error){
496+
conn := rc.pool.Get()
497+
defer conn.Close()
498+
val, err := redis.Bool(conn.Do("SMOVE", source, destination, member))
499+
return val, err
500+
}
501+
502+
func (rc *RedisClient) SUnion(key ...interface{}) ([]string, error){
311503
conn := rc.pool.Get()
312504
defer conn.Close()
505+
val, err := redis.Strings(conn.Do("SUNION", key...))
506+
return val, err
507+
}
313508

314-
val, err := redis.String(conn.Do("SREM", args...))
509+
func (rc *RedisClient) SUnionStore(destination string, key ...interface{})(int, error){
510+
conn := rc.pool.Get()
511+
defer conn.Close()
512+
args := append([]interface{}{destination}, key...)
513+
val, err := redis.Int(conn.Do("SUNIONSTORE", args))
315514
return val, err
316515
}
317516

517+
//****************** 全局操作 ***********************
518+
318519
// DBSize 返回当前数据库的 key 的数量
319520
func (rc *RedisClient) DBSize()(int64, error){
320521
conn := rc.pool.Get()

0 commit comments

Comments
 (0)