Permalink
Browse files

use one connection for login server

  • Loading branch information...
1 parent 2911cff commit 748b2015f540b60aa5056c705e6d3badf04f457f @vito committed Mar 14, 2012
Showing with 25 additions and 27 deletions.
  1. +23 −26 src/aliter/db.clj
  2. +2 −1 src/aliter/server/login.clj
View
@@ -4,41 +4,38 @@
(:import [aliter.data Account]))
-(defmacro with-redis [& body]
+(defmacro with-db [& body]
`(redis/with-server {}
~@body))
(defn save-account [account]
- (with-redis
- (redis/atomically
- (let [id (if (:id account)
- (:id account)
- (redis/incr "accounts:id"))
- acc (str "account:" id)]
- (redis/hset acc "login" (:login account))
- (redis/hset acc "password" (:password account))
- (redis/hset acc "email" (:email account))
- (redis/hset acc "gender" (:gender account))
- (redis/hset acc "last-login" (:last-login account))
- (redis/hset acc "last-ip" (:last-ip account))
+ (redis/atomically
+ (let [id (if (:id account)
+ (:id account)
+ (redis/incr "accounts:id"))
+ acc (str "account:" id)]
+ (redis/hset acc "login" (:login account))
+ (redis/hset acc "password" (:password account))
+ (redis/hset acc "email" (:email account))
+ (redis/hset acc "gender" (:gender account))
+ (redis/hset acc "last-login" (:last-login account))
+ (redis/hset acc "last-ip" (:last-ip account))
- (redis/hset (str "account:" (:login account)) id))))
+ (redis/hset (str "account:" (:login account)) id)))
account)
(defn get-account [id]
- (with-redis
- (let [acc (str "account:" id)]
- (Account.
- id
- (redis/hget acc "login")
- (redis/hget acc "password")
- (redis/hget acc "email")
- (Integer/parseInt (redis/hget acc "gender"))
- (redis/hget acc "last-login")
- (redis/hget acc "last-ip")))))
+ (let [acc (str "account:" id)]
+ (Account.
+ id
+ (redis/hget acc "login")
+ (redis/hget acc "password")
+ (redis/hget acc "email")
+ (Integer/parseInt (redis/hget acc "gender"))
+ (redis/hget acc "last-login")
+ (redis/hget acc "last-ip"))))
(defn get-account-id [login]
- (with-redis
- (Integer/parseInt (redis/get (str "account:" login)))))
+ (Integer/parseInt (redis/get (str "account:" login))))
@@ -28,4 +28,5 @@
this)))
(defn start [port]
- (server/run (LoginState. :normal login/packets) port))
+ (with-db
+ (server/run (LoginState. login/packets) port)))

0 comments on commit 748b201

Please sign in to comment.