Created
September 2, 2012 08:48
-
-
Save hellojinjie/3596018 to your computer and use it in GitHub Desktop.
top sync user session serivice
This file contains hidden or 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
package com.xindianbao.isv; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.concurrent.ExecutionException; | |
import com.google.common.cache.CacheBuilder; | |
import com.google.common.cache.CacheLoader; | |
import com.google.common.cache.LoadingCache; | |
import com.taobao.top.syncserver.Configuration; | |
import com.taobao.top.syncserver.service.ISessionService; | |
import com.taobao.top.syncserver.util.Constant; | |
/** | |
* 这里只是简单的读取 MySQL 的数据,所以就不用什么 ORM 工具了,直接用查询就可以了, | |
* 为了简单,这里不用连接池 | |
*/ | |
public class XinDianBaoSessionService implements ISessionService { | |
/** 这里我要读取 mysql 的配置 */ | |
private static Configuration configure = null; | |
private static LoadingCache<String, String> sessionCache = | |
CacheBuilder.newBuilder().build(new SessionCacheLoader());; | |
private static LoadingCache<Long, String> nickCache = | |
CacheBuilder.newBuilder().build(new NickCacheLoader());; | |
private static LoadingCache<String, Long> userIdCache = | |
CacheBuilder.newBuilder().build(new UserIdCacheLoader());; | |
public XinDianBaoSessionService(Configuration configure) { | |
synchronized (this) { | |
if (XinDianBaoSessionService.configure == null) { | |
XinDianBaoSessionService.configure = configure; | |
} | |
} | |
} | |
@Override | |
public String getSessionByNick(String appkey,String nick) { | |
String session = null; | |
try { | |
session = sessionCache.get(nick); | |
} catch (ExecutionException e) { | |
/* 如果捕获了异常就说明了没有找到 session,返回 null 就可以了 */ | |
} | |
return session; | |
} | |
@Override | |
public String getNickByUserId(Long userid) { | |
String nick = null; | |
try { | |
nick = nickCache.get(userid); | |
} catch (ExecutionException e) { | |
} | |
return nick; | |
} | |
@Override | |
public Long getUserIdByNick(String nick) { | |
Long userId = null; | |
try { | |
userId = userIdCache.get(nick); | |
} catch (ExecutionException e) { | |
} | |
return userId; | |
} | |
private static Connection getConnection() throws SQLException { | |
String url = configure.getConfig(Constant.DB_JDBC_URL); | |
String user = configure.getConfig(Constant.DB_USER_NAME); | |
String password = configure.getConfig(Constant.DB_PASSWORD); | |
return DriverManager.getConnection(url, user, password); | |
} | |
private static class SessionCacheLoader extends CacheLoader<String, String> { | |
private static final String SQL_GET_SESSION_BY_NICK = | |
"select top_session_key from session_key where nick = ?"; | |
@Override | |
public String load(String key) throws Exception { | |
Connection connection = null; | |
PreparedStatement statement = null; | |
try { | |
connection = getConnection(); | |
statement = connection.prepareStatement(SQL_GET_SESSION_BY_NICK); | |
statement.setString(1, key); | |
ResultSet rs = statement.executeQuery(); | |
if (rs.next()) { | |
return rs.getString(1); | |
} else { | |
throw new CacheLoader.InvalidCacheLoadException("no such session"); | |
} | |
} catch (SQLException ex) { | |
throw new CacheLoader.InvalidCacheLoadException(ex.getMessage()); | |
} finally { | |
try { | |
if (statement != null) { | |
statement.close(); | |
} | |
if (connection != null) { | |
connection.close(); | |
} | |
} catch (SQLException ex) { | |
} | |
} | |
} | |
} | |
private static class NickCacheLoader extends CacheLoader<Long, String> { | |
private static final String SQL_GET_NICK_BY_USERID = | |
"select nick from session_key where user_id = ?"; | |
@Override | |
public String load(Long key) throws Exception { | |
Connection connection = null; | |
PreparedStatement statement = null; | |
try { | |
connection = getConnection(); | |
statement = connection.prepareStatement(SQL_GET_NICK_BY_USERID); | |
statement.setString(1, String.valueOf(key)); | |
ResultSet rs = statement.executeQuery(); | |
if (rs.next()) { | |
return rs.getString(1); | |
} else { | |
throw new CacheLoader.InvalidCacheLoadException("no such session"); | |
} | |
} catch (SQLException ex) { | |
throw new CacheLoader.InvalidCacheLoadException(ex.getMessage()); | |
} finally { | |
try { | |
if (statement != null) { | |
statement.close(); | |
} | |
if (connection != null) { | |
connection.close(); | |
} | |
} catch (SQLException ex) { | |
} | |
} | |
} | |
} | |
private static class UserIdCacheLoader extends CacheLoader<String, Long> { | |
private static final String SQL_GET_USERID_BY_NICK = | |
"select user_id from session_key where nick = ?"; | |
@Override | |
public Long load(String key) throws Exception { | |
Connection connection = null; | |
PreparedStatement statement = null; | |
try { | |
connection = getConnection(); | |
statement = connection.prepareStatement(SQL_GET_USERID_BY_NICK); | |
statement.setString(1, String.valueOf(key)); | |
ResultSet rs = statement.executeQuery(); | |
if (rs.next()) { | |
return Long.valueOf(rs.getString(1)); | |
} else { | |
throw new CacheLoader.InvalidCacheLoadException("no such session"); | |
} | |
} catch (SQLException ex) { | |
throw new CacheLoader.InvalidCacheLoadException(ex.getMessage()); | |
} finally { | |
try { | |
if (statement != null) { | |
statement.close(); | |
} | |
if (connection != null) { | |
connection.close(); | |
} | |
} catch (SQLException ex) { | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment