redis-redigo及redis存储时间问题测试

时间:2023-03-10 08:29:35
redis-redigo及redis存储时间问题测试
package main

import (
"log" "github.com/garyburd/redigo/redis"
"github.com/garyburd/redigo/redisx"
) type MyStruct struct {
A int
B string
} type RequestLog struct {
RequestType string //Get,Put,Post,Delete
AccessKey string
SecretKey string
ReuqestURL string //请求网址
RemoteAddr string
CreateTime string //创建日期
} func main() {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
log.Fatal(err)
} // v0 := &MyStruct{1, "hello"}
v1 := &RequestLog{"GET", "a", "a", "/get/adsid/fasdfk", "19.2.23.2", "201403201528"}
// _, err = c.Do("HMSET", redisx.AppendStruct([]interface{}{"key"}, v0)...)
_, err = c.Do("HMSET", redisx.AppendStruct([]interface{}{"reqlog:201403201528.1"}, v1)...)
if err != nil {
log.Fatal(err)
} reply, err := c.Do("HGETALL", "reqlog")
if err != nil {
log.Fatal(err)
} v2 := &RequestLog{} err = redisx.ScanStruct(reply, v2)
if err != nil {
log.Fatal(err)
}
log.Printf("v2=%v", v2)
}
  • 在测试过程中发现redis无法存储时间类型数据,通过查阅资料,一般会将时间转换为符串做为key的一部分存在,以便于查询
  • 这时的问题是key最长允许存储多长:网上给的解释为1024字节(需查)
  • 表设计思路

redis-redigo及redis存储时间问题测试

优化: 节约内存:Instagram的Redis实践