Skip to content

Commit

Permalink
修改缓存数据库和关键词搜索的key
Browse files Browse the repository at this point in the history
  • Loading branch information
idoubi committed Jan 10, 2018
1 parent 3b64504 commit 2700ac1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func newClient() *redis.Client {
client = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
DB: cacheDB,
})
return client
}

// 设置新闻缓存
func setNewsCache(cache map[string]interface{}) error {
key0 := "gonews"
key0 := cachePrefix
key1 := ""
key2 := ""
if value, ok := cache["ctime"].(string); ok {
Expand Down Expand Up @@ -54,7 +54,7 @@ func getNewsCache(key string) (map[string]string, error) {

// 获取全部新闻
func getAllNews(page int64, per int64) ([]map[string]string, int64) {
key0 := "gonews"
key0 := cachePrefix
keys1, _ := client.SMembers(key0).Result()
newsList := map[string]map[string]string{}
for _, key1 := range keys1 {
Expand All @@ -80,7 +80,7 @@ func getAllNews(page int64, per int64) ([]map[string]string, int64) {

// 条件查询
func searchNews(keyword string, page int64, per int64) ([]map[string]string, int64) {
keys, _ := client.Keys("*" + keyword + "*").Result()
keys, _ := client.Keys(cachePrefix + "*" + keyword + "*").Result()
newsList := map[string]map[string]string{}
for _, key := range keys {
news, err := getNewsCache(key)
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"sync"
)

const (
cacheDB = 2 // 存储数据的redis db
cachePrefix = "gonews" // 缓存前缀
)

var dir string
var act string
var port int
Expand Down

0 comments on commit 2700ac1

Please sign in to comment.