This repository has been archived by the owner on Nov 29, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package api | ||
|
||
import ( | ||
"log" | ||
|
||
"fmt" | ||
|
||
"strconv" | ||
|
||
"github.com/mohuishou/scuplus-go/cache" | ||
) | ||
|
||
// 过期时间十分钟 | ||
const exp = 60 * 10 | ||
|
||
// Get get | ||
func Get(uid uint) int { | ||
v, err := cache.Do("GET", getKey(uid)) | ||
if err != nil { | ||
log.Println("get cache code err:", err) | ||
} | ||
val, ok := v.([]byte) | ||
if !ok { | ||
return 0 | ||
} | ||
n, _ := strconv.Atoi(string(val)) | ||
return n | ||
} | ||
|
||
// Add +1 | ||
func Add(uid uint) error { | ||
key := getKey(uid) | ||
_, err := cache.Do("INCR", key) | ||
if err != nil { | ||
log.Println("set cache code err:", err) | ||
} | ||
// 设置过期时间 | ||
_, err = cache.Do("Expire", key, exp) | ||
if err != nil { | ||
log.Println("set cache code err:", err) | ||
} | ||
return err | ||
} | ||
|
||
func getKey(uid uint) string { | ||
return fmt.Sprintf("api.count.%d", uid) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package api | ||
|
||
import "testing" | ||
|
||
func TestGet(t *testing.T) { | ||
t.Log(Get(1)) | ||
t.Log(Add(1)) | ||
t.Log(Get(1)) | ||
} |
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