Created
August 16, 2019 02:52
-
-
Save AK-10/1fbbdf534a019525af027bdc6090c50e to your computer and use it in GitHub Desktop.
redigoの利用例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setSheetsToRedis(sheets []Sheet) { | |
conn, err := redis.Dial("tcp", "localhost:6379") | |
if err != nil { | |
panic(err) | |
} | |
defer conn.Close() | |
// primitive型以外はjson.Marshalする | |
serialized, _ := json.Marshal(sheets) | |
conn.Do("SET", sheetKey, serialized) | |
} | |
func appendSheet(sheet Sheet) { | |
sheets := getAllSheetFromRedis() | |
sheets = append(sheets, sheet) | |
setSheetsToRedis(sheets) | |
} | |
func getAllSheetFromRedis() []Sheet { | |
conn, err := redis.Dial("tcp", "localhost:6379") | |
if err != nil { | |
panic(err) | |
} | |
defer conn.Close() | |
bytes, _ := redis.Bytes(conn.Do("GET", sheetKey)) | |
var deserialized []Sheet | |
json.Unmarshal(bytes, deserialized) | |
return deserialized | |
} | |
// key構成を頑張ってredisだけで走査する vs sliceをredisに持ってgo側で走査する | |
func findSheetWhere(condition func(s Sheet) bool) (bool, Sheet) { | |
sheets := getAllSheetFromRedis() | |
for _, v := range sheets { | |
if condition(v) { | |
return true, v | |
} | |
} | |
return false, Sheet{} | |
} | |
// limitつけてあげた方が良い? | |
func findSheetsWhere(condition func(s Sheet) bool) []Sheet { | |
sheets := getAllSheetFromRedis() | |
var resSheets []Sheet | |
for _, v := range sheets { | |
if condition(v) { | |
resSheets = append(resSheets, v) | |
} | |
} | |
return resSheets | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment