Skip to content

Commit

Permalink
feat(redis): redis controller more function
Browse files Browse the repository at this point in the history
* add overload reids.set with expire
* add hmset
  • Loading branch information
dreambo8563 committed Jan 23, 2018
1 parent 85a209b commit f31e653
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@types/serve-favicon": "^2.2.30",
"@types/winston": "^2.3.7",
"nodemon": "^1.14.3",
"tslint": "^5.9.1",
"typescript": "^2.6.2"
}
}
39 changes: 32 additions & 7 deletions src/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ export class RedisController {
this.keyTitle = keyTitle
this.redis = redis
}
async set(module: string, key: string, value: any) {
async set(module: string, key: string, value: any): Promise<any>
async set(
module: string,
key: string,
value: any,
expire: number
): Promise<any>
async set(module: string, key: string, value: any, expire?: number) {
try {
const keyPath = `${this.keyTitle}:${module}:${key}`
if (typeof expire === "number") {
return await this.redis.setex(keyPath, expire, JSON.stringify(value))
}
return await this.redis.set(keyPath, JSON.stringify(value))
} catch (e) {
Logger.error("error when save redis , error info is :%", e)
Expand All @@ -26,7 +36,7 @@ export class RedisController {
}
return JSON.parse(value)
} catch (e) {
Logger.error("error when read redis , error info is :%", e)
Logger.error("error when read redis , error info is :%", e, key)
}
}

Expand All @@ -41,13 +51,28 @@ export class RedisController {
const toBeSavedObj: string[] = []
Object.keys(value).forEach(key => {
const keyPath = `${this.keyTitle}:${module}:${key}`
toBeSavedObj.push(keyPath)
toBeSavedObj.push(JSON.stringify(value[key]))
toBeSavedObj.push(keyPath, JSON.stringify(value[key]))
// toBeSavedObj.push(JSON.stringify(value[key]))
})
const [a, b, ...arr] = toBeSavedObj
return await this.redis.mset(a, b, ...arr)
} catch (e) {
Logger.error("error when multi save redis , error info is :%", e)
Logger.error("error when multiSet redis , error info is :%", e, value)
}
}
async multHashSet(
module: string,
hash: string,
value: { [key: string]: string }
) {
try {
if (Object.keys(value).length < 1) {
throw new Error("multHashSet at least have one key")
}
const keyPath = `${this.keyTitle}:${module}:${hash}`
return await this.redis.hmset(keyPath, value)
} catch (e) {
Logger.error("error when multHashSet redis , error info is :%", e, value)
}
}

Expand Down Expand Up @@ -78,7 +103,7 @@ export class RedisController {
})
return result
} catch (e) {
Logger.error("error when multi read redis , error info is :%", e)
Logger.error("error when multi read redis , error info is :%", e, key)
}
}
async delete(module: string, key: string) {
Expand All @@ -87,7 +112,7 @@ export class RedisController {
console.log(keyPath, "del")
return await this.redis.del(keyPath)
} catch (e) {
Logger.error("error when delete redis , error info is :%", e)
Logger.error("error when delete redis , error info is :%", e, key)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/routes/todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ router.post("/", async (req, res) => {
// save it to db
const ids = await Todo.createToDo(todoItem)
// Response.respondJSON(res, false, { test: "testmsg" })
// await redis.set("todo", R.head(ids).toString(), todoItem.name)
await redis.multiSet(`todo:${R.head(ids)}`, Object({ name: todoItem.name }))
// await redis.set("todo", R.head(ids).toString(), todoItem.name, 60)
// await redis.multiSet(`todo:${R.head(ids)}`, Object({ name: todoItem.name }))
await redis.multHashSet("todo", String(R.head(ids)), Object(todoItem))
Response.respondJSON(res, true, todoItem)
} catch (error) {
Response.respondJSON(res, false, error.message)
Expand Down

0 comments on commit f31e653

Please sign in to comment.