本文整理匯總了Golang中github.com/garyburd/redigo/redis.Int函數的典型用法代碼示例。如果您正苦於以下問題:Golang Int函數的具體用法?Golang Int怎麽用?Golang Int使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Int函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestHashSetNX
func TestHashSetNX(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
// New Hash
v, err := redis.Int(c.Do("HSETNX", "wim", "zus", "jet"))
ok(t, err)
equals(t, 1, v)
v, err = redis.Int(c.Do("HSETNX", "wim", "zus", "jet"))
ok(t, err)
equals(t, 0, v)
// Just a new key
v, err = redis.Int(c.Do("HSETNX", "wim", "aap", "noot"))
ok(t, err)
equals(t, 1, v)
// Wrong key type
s.Set("foo", "bar")
_, err = redis.Int(c.Do("HSETNX", "foo", "nosuch", "nosuch"))
assert(t, err != nil, "no HSETNX error")
}
示例2: CreateURL
// creates the requested shortened URL
func CreateURL(conn redis.Conn, longURL string, shortURL string) (string, error) {
// randomly assign URL if not given
if shortURL == "" {
for { // loop until unique string
shortURL = randURL()
v, err := redis.Int(conn.Do("EXISTS", shortURL))
if err == nil && v == 0 {
break
}
}
} else { // confirm that URL is free
v, err := redis.Int(conn.Do("EXISTS", shortURL))
if err != nil {
return "", err
} else if v == 1 {
return "", UrlInUse
}
}
// create hash record
v, err := redis.String(conn.Do("HMSET", shortURL, LONG, longURL, COUNT, 0))
if v != "OK" || err != nil {
return "", err
}
return shortURL, err
}
示例3: TestHashExists
func TestHashExists(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
s.HSet("wim", "zus", "jet")
s.HSet("wim", "teun", "vuur")
v, err := redis.Int(c.Do("HEXISTS", "wim", "zus"))
ok(t, err)
equals(t, 1, v)
v, err = redis.Int(c.Do("HEXISTS", "wim", "nosuch"))
ok(t, err)
equals(t, 0, v)
v, err = redis.Int(c.Do("HEXISTS", "nosuch", "nosuch"))
ok(t, err)
equals(t, 0, v)
// Wrong key type
s.Set("foo", "bar")
_, err = redis.Int(c.Do("HEXISTS", "foo", "nosuch"))
assert(t, err != nil, "no HDEL error")
}
示例4: TestUnsetTwoCNames
func (s *S) TestUnsetTwoCNames(c *check.C) {
router := hipacheRouter{prefix: "hipache"}
err := router.AddBackend("myapp")
c.Assert(err, check.IsNil)
err = router.SetCName("myapp.com", "myapp")
c.Assert(err, check.IsNil)
cnames, err := redis.Int(conn.Do("LLEN", "cname:myapp"))
c.Assert(err, check.IsNil)
c.Assert(1, check.Equals, cnames)
err = router.SetCName("myapptwo.com", "myapp")
c.Assert(err, check.IsNil)
cnames, err = redis.Int(conn.Do("LLEN", "cname:myapp"))
c.Assert(err, check.IsNil)
c.Assert(2, check.Equals, cnames)
err = router.UnsetCName("myapp.com", "myapp")
c.Assert(err, check.IsNil)
cnames, err = redis.Int(conn.Do("LLEN", "cname:myapp"))
c.Assert(err, check.IsNil)
c.Assert(1, check.Equals, cnames)
err = router.UnsetCName("myapptwo.com", "myapp")
c.Assert(err, check.IsNil)
cnames, err = redis.Int(conn.Do("LLEN", "cname:myapp"))
c.Assert(err, check.IsNil)
c.Assert(0, check.Equals, cnames)
}
示例5: CreateUser
func (db *DB) CreateUser(login, name string) (int, error) {
// allocate a connection
c := db.Get()
defer c.Close()
// check if username is taken
if exists, err := redis.Int(c.Do("HEXISTS", "users:", login)); err != nil || exists == 1 {
return -1, err
}
// increment global user count
id, err := redis.Int(c.Do("INCR", "user:id"))
if err != nil {
return -1, err
}
// we want to do a transaction so we use MULTI cmd1 cmd2 ... EXEC
c.Do("MULTI")
// register the username to the uid
c.Do("HSET", "users:", login, id)
// set fields for user structure
c.Do("HMSET", "user:"+strconv.Itoa(id), "login", login,
"id", id, "name", name, "followers", "0", "following", "0",
"posts", "0", "signup", time.Now().Unix())
if _, err := c.Do("EXEC"); err != nil {
return -1, err
}
return id, nil
}
示例6: Queue
func (this *tallySubscriberImpl) Queue(queue string, inbound <-chan interface{}) {
go func() {
var queueLength int = 0
var err error
queueLength, _ = redis.Int(this.queue.Do("LLEN", queue))
for {
select {
case message := <-inbound:
if queueLength >= this.settings.MaxQueueLength {
// do a read of the length in the hope that at some point the queue starts to drain
queueLength, err = redis.Int(this.queue.Do("LLEN", queue))
if err != nil {
glog.Warningln("error-llen", queue, err, this.settings)
}
if queueLength >= this.settings.MaxQueueLength {
glog.Warningln("queue-length-exceeds-limit", this.settings.MaxQueueLength, queueLength)
// drop the message
continue
}
}
queueLength, err = redis.Int(this.queue.Do("LPUSH", queue, message))
if err != nil {
glog.Warningln("error-lpush", queue, err, this.settings)
}
case stop := <-this.stop:
if stop {
return
}
}
}
}()
}
示例7: addResultsToWinsAndLosses
func addResultsToWinsAndLosses(resultString string, gameSessionPair lib.GameSessionPair, r redis.Conn) {
var winningIndex, losingIndex int
if resultString == "tie" {
_, err := redis.Int(r.Do("SADD", gameSessionPair[0].GetTieRedisKey(), gameSessionPair[1].ID.Hex()))
if err != nil {
panic(err)
}
_, err = redis.Int(r.Do("SADD", gameSessionPair[1].GetTieRedisKey(), gameSessionPair[0].ID.Hex()))
if err != nil {
panic(err)
}
} else {
switch resultString {
case "humans":
winningIndex = 0
losingIndex = 1
case "ogres":
winningIndex = 1
losingIndex = 0
default:
fmt.Println("You screwed up with the switch, buddy")
}
_, err := redis.Int(r.Do("SADD", gameSessionPair[winningIndex].GetWinningRedisKey(), gameSessionPair[losingIndex].ID.Hex()))
if err != nil {
panic(err)
}
_, err = redis.Int(r.Do("SADD", gameSessionPair[losingIndex].GetLosingRedisKey(), gameSessionPair[winningIndex].ID.Hex()))
if err != nil {
panic(err)
}
}
}
示例8: TestZSetRank
func TestZSetRank(t *testing.T) {
startTestApp()
c := getTestConn()
defer c.Close()
key := []byte("myzset")
if _, err := redis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
t.Fatal(err)
}
if n, err := redis.Int(c.Do("zrank", key, "c")); err != nil {
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
if _, err := redis.Int(c.Do("zrank", key, "e")); err != redis.ErrNil {
t.Fatal(err)
}
if n, err := redis.Int(c.Do("zrevrank", key, "c")); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
if _, err := redis.Int(c.Do("zrevrank", key, "e")); err != redis.ErrNil {
t.Fatal(err)
}
}
示例9: GetLength
// 返回名稱為key的list的長度
func (this *RedisListHelper) GetLength(key string) int {
var (
n int
err error
)
if atomic.CompareAndSwapInt32(&this.readMark1, 0, 1) {
n, err = redis.Int(this.readCon1.Do("LLEN", key))
atomic.StoreInt32(&this.readMark1, 0)
} else if atomic.CompareAndSwapInt32(&this.readMark2, 0, 1) {
n, err = redis.Int(this.readCon2.Do("LLEN", key))
atomic.StoreInt32(&this.readMark2, 0)
} else if atomic.CompareAndSwapInt32(&this.readMark3, 0, 1) {
n, err = redis.Int(this.readCon3.Do("LLEN", key))
atomic.StoreInt32(&this.readMark3, 0)
} else if atomic.CompareAndSwapInt32(&this.readMark4, 0, 1) {
n, err = redis.Int(this.readCon4.Do("LLEN", key))
atomic.StoreInt32(&this.readMark4, 0)
}
if err != nil {
return 0
}
return n
}
示例10: TestListMPush
func TestListMPush(t *testing.T) {
startTestApp()
c := getTestConn()
defer c.Close()
key := []byte("b")
if n, err := redis.Int(c.Do("rpush", key, 1, 2, 3)); err != nil {
t.Fatal(err)
} else if n != 3 {
t.Fatal(n)
}
if err := testListRange(key, 0, 3, 1, 2, 3); err != nil {
t.Fatal(err)
}
if n, err := redis.Int(c.Do("lpush", key, 1, 2, 3)); err != nil {
t.Fatal(err)
} else if n != 6 {
t.Fatal(n)
}
if err := testListRange(key, 0, 6, 3, 2, 1, 1, 2, 3); err != nil {
t.Fatal(err)
}
}
示例11: GetMsgNum
// 獲取uuid的離線消息數量
func GetMsgNum(uuid string) int {
var (
n int
err error
)
if atomic.CompareAndSwapInt32(&offlinemsg_readMark1, 0, 1) {
n, err = redis.Int(offlinemsg_readCon1.Do("LLEN", uuid))
atomic.StoreInt32(&offlinemsg_readMark1, 0)
} else if atomic.CompareAndSwapInt32(&offlinemsg_readMark2, 0, 1) {
n, err = redis.Int(offlinemsg_readCon2.Do("LLEN", uuid))
atomic.StoreInt32(&offlinemsg_readMark2, 0)
} else if atomic.CompareAndSwapInt32(&offlinemsg_readMark3, 0, 1) {
n, err = redis.Int(offlinemsg_readCon3.Do("LLEN", uuid))
atomic.StoreInt32(&offlinemsg_readMark3, 0)
} else if atomic.CompareAndSwapInt32(&offlinemsg_readMark4, 0, 1) {
n, err = redis.Int(offlinemsg_readCon4.Do("LLEN", uuid))
atomic.StoreInt32(&offlinemsg_readMark4, 0)
}
if err != nil {
return 0
}
return n
}
示例12: TestEnqueue
func TestEnqueue(t *testing.T) {
conn := pool.Get()
defer conn.Close()
job.Enqueue(pool)
expected := fmt.Sprintf(`{"jid":"%s","retry":false,"queue":"default","class":"HardWorder","args":[],"enqueued_at":%d}`,
job.JID,
job.EnqueuedAt)
actual := job.toJSON()
if expected != actual {
t.Errorf("Excepted JSON to be %s, got %s", expected, job.toJSON())
}
count, _ := redis.Int(conn.Do("SISMEMBER", "queues", job.Queue))
if count != 1 {
t.Error("Expected queues list to have the correct queue but didn't found it.")
}
count, _ = redis.Int(conn.Do("LLEN", "queue:default"))
if count != 1 {
t.Errorf("Expected the queue to have exactly one job but found %d", count)
}
resetRedis(pool)
}
示例13: TestRedisCache
func TestRedisCache(t *testing.T) {
bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
if err != nil {
t.Error("init err")
}
if err = bm.Put("astaxie", 1, 10); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
time.Sleep(10 * time.Second)
if bm.IsExist("astaxie") {
t.Error("check err")
}
if err = bm.Put("astaxie", 1, 10); err != nil {
t.Error("set Error", err)
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
t.Error("get err")
}
if err = bm.Incr("astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
t.Error("get err")
}
if err = bm.Decr("astaxie"); err != nil {
t.Error("Decr Error", err)
}
if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
t.Error("get err")
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
t.Error("delete err")
}
//test string
if err = bm.Put("astaxie", "author", 10); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
t.Error("get err")
}
// test clear all
if err = bm.ClearAll(); err != nil {
t.Error("clear all err")
}
}
示例14: TestLlen
func TestLlen(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
s.Push("l", "aap", "noot", "mies", "vuur")
{
el, err := redis.Int(c.Do("LLEN", "l"))
ok(t, err)
equals(t, 4, el)
}
// Non exising key
{
el, err := redis.Int(c.Do("LLEN", "nonexisting"))
ok(t, err)
equals(t, 0, el)
}
// Wrong type of key
{
_, err := redis.String(c.Do("SET", "str", "value"))
ok(t, err)
_, err = redis.Int(c.Do("LLEN", "str"))
assert(t, err != nil, "LLEN error")
// Too many arguments
_, err = redis.String(c.Do("LLEN", "too", "many"))
assert(t, err != nil, "LLEN error")
}
}
示例15: TestHLen
func TestHLen(t *testing.T) {
c := NewFakeRedis()
c.Do("HSET", "foo", "k1", "v1")
c.Do("HSET", "foo", "k2", "v2")
assertInt(t, must(redis.Int(c.Do("HLEN", "foo"))), 2)
assertInt(t, must(redis.Int(c.Do("HLEN", "empty"))), 0)
}