Mastering Go Redis Set Operations: API Guide and Performance Benchmark
This article provides a comprehensive guide to implementing and testing Go Redis Set APIs—including SAdd, SCard, SIsMember, SMembers, SRem, SPop, and SPopN—complete with code examples, a unified test suite, and a performance benchmark that reports QPS and execution time.
Continuing a series on building a Go‑based Redis operation API and measuring its performance, this post focuses on the Set data type, reviewing common Set APIs, presenting implementation code, and evaluating both functional correctness and throughput.
SAdd
The SAdd method adds one or more members to a set and returns the number of newly added elements; duplicate members are ignored.
// SAdd
// @Description: add set elements
// @receiver r
// @param key
// @param members
// @return int64
func (r RedisBase) SAdd(key string, members ...interface{}) int64 {
result, err := r.pool.SAdd(key, members...).Result()
if err != nil {
log.Printf("sadd key:%s,members:%s fail
", key, ftool.ToString(members))
log.Println(err)
return base.TestError
}
return result
}SCard
SCardreturns the cardinality (number of elements) of a set.
// SCard
// @Description: get set size
// @receiver r
// @param key
// @return int64
func (r RedisBase) SCard(key string) int64 {
result, err := r.pool.SCard(key).Result()
if err != nil {
log.Printf("scard key:%s fail
", key)
log.Println(err)
return base.TestError
}
return result
}SIsMember
SIsMemberchecks whether a given element exists in the set, returning a boolean.
// SIsMember
// @Description: check element existence
// @receiver r
// @param key
// @param member
// @return bool
func (r RedisBase) SIsMember(key string, member interface{}) bool {
result, err := r.pool.SIsMember(key, member).Result()
if err != nil {
log.Printf("sismember key:%s,member:%s fail
", key, ftool.ToString(member))
log.Println(err)
return false
}
return result
}SMembers
SMembersretrieves all elements of a set as a slice of strings.
// SMembers
// @Description: get all set elements
// @receiver r
// @param key
// @return []string
func (r RedisBase) SMembers(key string) []string {
result, err := r.pool.SMembers(key).Result()
if err != nil {
log.Printf("smember key:%s fail
", key)
log.Println(err)
return nil
}
return result
}SRem
SRemremoves one or more members from a set and returns the count of successfully removed elements.
// SRem
// @Description: delete set elements
// @receiver r
// @param key
// @param members
func (r RedisBase) SRem(key string, members ...interface{}) int64 {
result, err := r.pool.SRem(key, members...).Result()
if err != nil {
log.Printf("srem key:%s members:%s fail
", key, ftool.ToString(members))
log.Println(err)
return base.TestError
}
return result
}SPop & SPopN
SPoprandomly removes and returns a single element from the set; SPopN removes and returns up to N random elements, returning fewer if the set contains less than N.
// SPop
// @Description: pop a random element and delete it
// @receiver r
// @param key
// @return string
func (r RedisBase) SPop(key string) string {
result, err := r.pool.SPop(key).Result()
if err != nil {
log.Printf("spop key:%s fail
", key)
log.Println(err)
return base.Empty
}
return result
}API Demonstration Test Case
The following test aggregates all Set APIs into a single example, illustrating typical usage patterns.
func TestSet(t *testing.T) {
var pool = redis.NewRdisPool("127.0.0.1:6379", base.Empty, 1)
var key = "funset"
add := pool.SAdd(key, ftool.RandomStr(10), ftool.RandomStr(3), "FunTester")
log.Println(add)
log.Printf("FunTester exists? %t", pool.SIsMember(key, "FunTester"))
log.Println(pool.SCard(key))
members := pool.SMembers(key)
for _, s := range members {
log.Println(s)
if ftool.RandomInt(2) == 1 && s != "FunTester" {
pool.SRem(key, s)
}
}
log.Println(pool.SRem(key, "FunTester", "000000000"))
log.Println(pool.SPop(key))
log.Println(len(pool.SPopN(key, 1000)))
}Performance Test
The performance benchmark adds random strings to a set, then repeatedly pops elements, measuring total execution time and QPS.
func TestSetPer(t *testing.T) {
var pool = redis.NewRdisPool("127.0.0.1:6379", base.Empty, 1)
var key = "funsetper"
execute.ExecuteRoutineTime(func() {
add := int(pool.SAdd(key, ftool.RandomStr(10), ftool.RandomStr(3)))
for i := 0; i < add; i++ {
pool.SPop(key)
}
}, 20, 5)
}Sample console output shows a total duration of ~20 seconds, 330,634 requests processed, and a QPS of approximately 16,500, demonstrating the throughput of the Set operations under the test conditions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
