本文整理匯總了Golang中github.com/garyburd/redigo/redis.Float64函數的典型用法代碼示例。如果您正苦於以下問題:Golang Float64函數的具體用法?Golang Float64怎麽用?Golang Float64使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Float64函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getResponseTime
func (n *node) getResponseTime() float64 {
f, err := redis.Float64(do("GET", fmt.Sprintf("nodes.%s.avg", n.name)))
if err != nil {
logrus.Error(err)
}
return f
}
示例2: updateServerLoad
func updateServerLoad(pool *redis.Pool, serverID string) {
tock := time.Duration((common.SERVER_UPDATE_LOAD_SECONDS * 0.95) * float64(time.Second))
c := time.Tick(tock) //want it to run SLIGHTLY before the key expires
conn := pool.Get()
for _ = range c { //inf for loop
fmt.Println("Setting server load...")
if _, err := conn.Do("PEXPIRE", "ASMS::server:"+serverID+"::load:", common.SERVER_UPDATE_LOAD_SECONDS*1000); err != nil {
panic(err)
} //end if
numberOfDevices, err := redis.Float64(conn.Do("LLEN", "ASMS::server:"+serverID+"::deviceIDs:"))
if err != nil {
panic(err) //could run into an issue here as this might not be set. Might need to set it to zero.
} // no issue it seems, redis returns a 0 if the list key doesnt exist.
currentLoad := numberOfDevices * common.MAX_DEVICES_LOAD_DENOMINATOR // % out of 100
//current load
if _, err := conn.Do("SET", "ASMS::server:"+serverID+"::load:", currentLoad); err != nil {
panic(err)
} //end if
fmt.Println("Done setting server load...")
} //end for loop
} //end UpdateServerLoad
示例3: getFill
func (n *node) getFill() float64 {
f, err := redis.Float64(do("GET", fmt.Sprintf("nodes.%s.fill", n.name)))
if err != nil {
logrus.Error(err)
}
return f
}
示例4: pipelineScore
func pipelineScore(conn redis.Conn, keyMembers []common.KeyMember) (map[common.KeyMember]Presence, error) {
for _, keyMember := range keyMembers {
if err := conn.Send("ZSCORE", keyMember.Key+insertSuffix, keyMember.Member); err != nil {
return map[common.KeyMember]Presence{}, err
}
if err := conn.Send("ZSCORE", keyMember.Key+deleteSuffix, keyMember.Member); err != nil {
return map[common.KeyMember]Presence{}, err
}
}
if err := conn.Flush(); err != nil {
return map[common.KeyMember]Presence{}, err
}
m := map[common.KeyMember]Presence{}
for i := 0; i < len(keyMembers); i++ {
insertReply, insertErr := conn.Receive()
insertValue, insertErr := redis.Float64(insertReply, insertErr)
deleteReply, deleteErr := conn.Receive()
deleteValue, deleteErr := redis.Float64(deleteReply, deleteErr)
switch {
case insertErr == nil && deleteErr == redis.ErrNil:
m[keyMembers[i]] = Presence{
Present: true,
Inserted: true,
Score: insertValue,
}
case insertErr == redis.ErrNil && deleteErr == nil:
m[keyMembers[i]] = Presence{
Present: true,
Inserted: false,
Score: deleteValue,
}
case insertErr == redis.ErrNil && deleteErr == redis.ErrNil:
m[keyMembers[i]] = Presence{
Present: false,
}
default:
return map[common.KeyMember]Presence{}, fmt.Errorf(
"pipelineScore bad state for %v (%v/%v)",
keyMembers[i],
insertErr,
deleteErr,
)
}
}
return m, nil
}
示例5: handleReply
//when a device requests something from the tracker this is what responds
func handleReply(pool *redis.Pool, zmpREP *zmq.Socket) {
for { //endless loop
req, err := zmpREP.Recv()
if err != nil {
fmt.Println(err)
} //end if
var rMsg string //the return message
if len(req) == 1 { //the message is only 1 part long.
msg := string(req[0])
if msg == "serverLoads" { //if they sent the right message to the tracker
conn := pool.Get() //get a connection from the pool
defer conn.Close()
//get the list of server loads
//// Start getting the server loads from redis
listLength, err := redis.Int(conn.Do("LLEN", "ASMS::connected::serverID:"))
if err != nil {
fmt.Println(err)
}
sIDs, err := redis.Strings(conn.Do("LRANGE", "ASMS::connected::serverID:", 0, listLength))
if err != nil {
fmt.Println(err)
}
serverLoads := []*common.Server{}
for i := range sIDs {
load, err := redis.Float64(conn.Do("GET", "ASMS::server:"+sIDs[i]+"::load:"))
if err != nil {
fmt.Println(err)
}
serverLoads = append(serverLoads, &common.Server{ServerID: sIDs[i], ServerLoad: common.Load(load)}) //add the server to the list, assert the load as common.Load
}
sort.Sort(common.ByLoad{serverLoads}) //sort the loads of the servers
//translate the serverID to the servers IP
serverIP, err := redis.String(conn.Do("GET", "ASMS::serverID:"+serverLoads[0].ServerID+"::IP:"))
if err != nil {
fmt.Println(err)
}
rMsg = serverIP //set rMsg equal to the serverID ip with the smallest load
} else {
rMsg = "0" // we will program this in as being unknown message recieved
} //end if
err = zmpREP.Send([][]byte{ //send the response back
[]byte(rMsg),
})
if err != nil { //check for an error
panic(err)
} //end if
} //end if
} //end for
} //end handleReply
示例6: incrementCounterInternal
func (db *Database) incrementCounterInternal(key string, delta float64, t time.Time) (float64, error) {
// nt = n0 * math.Exp(-lambda * t)
// lambda = math.Ln2 / thalf
c := db.Pool.Get()
defer c.Close()
const lambda = math.Ln2 / float64(counterHalflife)
scaledTime := lambda * float64(t.Sub(time.Unix(1257894000, 0)))
return redis.Float64(incrementCounterScript.Do(c, key, delta, scaledTime, (4*counterHalflife)/time.Second))
}
示例7: TestSortedSetIncrby
// Test ZINCRBY
func TestSortedSetIncrby(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
// Normal cases
{
// New key
b, err := redis.Float64(c.Do("ZINCRBY", "z", 1, "member"))
ok(t, err)
equals(t, 1.0, b)
// Existing key
b, err = redis.Float64(c.Do("ZINCRBY", "z", 2.5, "member"))
ok(t, err)
equals(t, 3.5, b)
// New member
b, err = redis.Float64(c.Do("ZINCRBY", "z", 1, "othermember"))
ok(t, err)
equals(t, 1.0, b)
}
// Error cases
{
_, err = redis.String(c.Do("ZINCRBY"))
assert(t, err != nil, "ZINCRBY error")
_, err = redis.String(c.Do("ZINCRBY", "set"))
assert(t, err != nil, "ZINCRBY error")
_, err = redis.String(c.Do("ZINCRBY", "set", "nofloat", "a"))
assert(t, err != nil, "ZINCRBY error")
_, err = redis.String(c.Do("ZINCRBY", "set", 1.0, "too", "many"))
assert(t, err != nil, "ZINCRBY error")
// Wrong type of key
s.Set("str", "value")
_, err = c.Do("ZINCRBY", "str", 1.0, "member")
assert(t, err != nil, "ZINCRBY error")
}
}
示例8: TestSortedSetScore
// Test ZSCORE
func TestSortedSetScore(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
s.ZAdd("z", 1, "one")
s.ZAdd("z", 2, "two")
s.ZAdd("z", 2, "zwei")
// Simple case
{
b, err := redis.Float64(c.Do("ZSCORE", "z", "two"))
ok(t, err)
equals(t, 2.0, b)
}
// no such member
{
b, err := c.Do("ZSCORE", "z", "nosuch")
ok(t, err)
equals(t, nil, b)
}
// no such key
{
b, err := c.Do("ZSCORE", "nosuch", "nosuch")
ok(t, err)
equals(t, nil, b)
}
// Direct
{
s.ZAdd("z2", 1, "one")
s.ZAdd("z2", 2, "two")
score, err := s.ZScore("z2", "two")
ok(t, err)
equals(t, 2.0, score)
}
// Error cases
{
_, err = redis.String(c.Do("ZSCORE"))
assert(t, err != nil, "ZSCORE error")
_, err = redis.String(c.Do("ZSCORE", "key"))
assert(t, err != nil, "ZSCORE error")
_, err = redis.String(c.Do("ZSCORE", "too", "many", "arguments"))
assert(t, err != nil, "ZSCORE error")
// Wrong type of key
s.Set("str", "value")
_, err = redis.Int(c.Do("ZSCORE", "str", "aap"))
assert(t, err != nil, "ZSCORE error")
}
}
示例9: TestPopular
func TestPopular(t *testing.T) {
db := newDB(t)
defer closeDB(db)
c := db.Pool.Get()
defer c.Close()
// Add scores for packages. On each iteration, add half-life to time and
// divide the score by two. All packages should have the same score.
now := time.Now()
score := float64(4048)
for id := 12; id >= 0; id-- {
path := "github.com/user/repo/p" + strconv.Itoa(id)
c.Do("SET", "id:"+path, id)
_, err := incrementPopularScore.Do(c, path, score, scaledTime(now))
if err != nil {
t.Fatal(err)
}
now = now.Add(popularHalfLife)
score /= 2
}
values, _ := redis.Values(c.Do("ZRANGE", "popular", "0", "100000", "WITHSCORES"))
if len(values) != 26 {
t.Errorf("Expected 26 values, got %d", len(values))
}
// Check for equal scores.
score, err := redis.Float64(values[1], nil)
if err != nil {
t.Fatal(err)
}
for i := 3; i < len(values); i += 2 {
s, _ := redis.Float64(values[i], nil)
if math.Abs(score-s)/score > 0.0001 {
t.Errorf("Bad score, score[1]=%g, score[%d]=%g", score, i, s)
}
}
}
示例10: SortedSetIncrBy
// SortedSetIncrBy increments the value of a member
// in a sorted set
//
// This function tries to return last floating value of the item,
// if it fails to parse reply to float64, returns parsing error along with
// Reply it self
func (r *RedisSession) SortedSetIncrBy(key string, incrBy, item interface{}) (float64, error) {
prefixed := make([]interface{}, 0)
// add key
prefixed = append(prefixed, r.AddPrefix(key))
// add incrBy
prefixed = append(prefixed, incrBy)
// add item
prefixed = append(prefixed, item)
return redis.Float64(r.Do("ZINCRBY", prefixed...))
}
示例11: TestAddJobToSetScript
func TestAddJobToSetScript(t *testing.T) {
testingSetUp()
defer testingTeardown()
// Create a test job
jobs, err := createAndSaveTestJobs(1)
if err != nil {
t.Fatalf("Unexpected error in createAndSaveTestJobs: %s", err.Error())
}
job := jobs[0]
// Add the job to the time index with a score of 7 days ago
tx := newTransaction()
expectedScore := float64(time.Now().Add(-7 * 24 * time.Hour).UTC().UnixNano())
tx.addJobToSet(job, Keys.JobsTimeIndex, expectedScore)
if err := tx.exec(); err != nil {
t.Errorf("Unexpected err in tx.exec(): %s", err.Error())
}
// Make sure the job was added to the set properly
conn := redisPool.Get()
defer conn.Close()
score, err := redis.Float64(conn.Do("ZSCORE", Keys.JobsTimeIndex, job.id))
if err != nil {
t.Errorf("Unexpected error in ZSCORE: %s", err.Error())
}
if score != expectedScore {
t.Errorf("Score in time index set was incorrect. Expected %f but got %f", expectedScore, score)
}
// Destroy the job and make sure the script does not add it to a new set
if err := job.Destroy(); err != nil {
t.Errorf("Unexpected err in job.Destroy: %s", err.Error())
}
tx = newTransaction()
tx.addJobToSet(job, "fooSet", 42.0)
if err := tx.exec(); err != nil {
t.Errorf("Unexpected err in tx.exec(): %s", err.Error())
}
exists, err := redis.Bool(conn.Do("EXISTS", "fooSet"))
if err != nil {
t.Errorf("Unexpected err in EXISTS: %s", err.Error())
}
if exists {
t.Error("Expected fooSet to not exist after the job was destroyed but it did.")
}
}
示例12: Score
func (s *SortedSetType) Score(member string) float64 {
reply, err := s.client.do("ZSCORE", s.Key, member)
score, _ := redis.Float64(reply, err)
return score
}
示例13: TestReply
ve(redis.Strings(nil, nil)),
ve([]string(nil), redis.ErrNil),
},
{
"values([v1, v2])",
ve(redis.Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
ve([]interface{}{[]byte("v1"), []byte("v2")}, nil),
},
{
"values(nil)",
ve(redis.Values(nil, nil)),
ve([]interface{}(nil), redis.ErrNil),
},
{
"float64(1.0)",
ve(redis.Float64([]byte("1.0"), nil)),
ve(float64(1.0), nil),
},
{
"float64(nil)",
ve(redis.Float64(nil, nil)),
ve(float64(0.0), redis.ErrNil),
},
}
func TestReply(t *testing.T) {
for _, rt := range replyTests {
if rt.actual.err != rt.expected.err {
t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err)
continue
}
示例14: SortedSetScore
// SortedSetScore returns score of a member in a sorted set. If no member,
// an error is returned.
//
// See: http://redis.io/commands/zscore
func (r *RedisSession) SortedSetScore(key string, member interface{}) (float64, error) {
return redis.Float64(r.Do("ZSCORE", r.AddPrefix(key), member))
}
示例15: GetCoverage
func GetCoverage(c redis.Conn, repo string) (float64, error) {
return redis.Float64(c.Do("GET", repo+".coverage"))
}