Skip to content

Commit

Permalink
update redis client
Browse files Browse the repository at this point in the history
  • Loading branch information
jarjin committed Mar 19, 2023
1 parent 038aca0 commit 5013932
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Binary file added FirServer/FirServer/lib/jedis-4.4.0-m2.jar
Binary file not shown.
8 changes: 7 additions & 1 deletion FirServer/FirServer/src/com/define/AppConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public class AppConst {
///数据库类型
public final static DBType DbType = DBType.None;
public final static boolean USE_REDIS = false;
/** 是否使用REDIS做缓存*/
public final static boolean USE_REDIS_CACHE = false;

/** mysql url*/
public final static String MYSQL_URL = "jdbc:mysql://localhost:3306/";
Expand All @@ -22,6 +23,11 @@ public class AppConst {
public final static int MONGO_PORT = 27017;
/** mongodb datebase */
public final static String MONGO_DBNAME = "firdatabase";

/** redis ip*/
public final static String REDIS_IP = "localhost";
/** redis port*/
public final static int REDIS_PORT = 6379;

public final static String CONFIG_PATH = "";
public final static String CITY_NAME = "city.xml";
Expand Down
1 change: 0 additions & 1 deletion FirServer/FirServer/src/com/helper/MySQLHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public Object get(String dbname, String uid, String key) {
}

public void set(String tbname, String uid, String key, Object value) {
String strKey = tbname + "_" + uid + "_" + key;
executeUpdate("update " + tbname + " set " + key + "=" + value + " where userid='" + uid + "'");
}

Expand Down
33 changes: 26 additions & 7 deletions FirServer/FirServer/src/com/helper/RedisHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@

import com.define.AppConst;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

//https://github.com/redis/jedis
public class RedisHelper {
private JedisPool pool = null;
private Jedis client = null;

public void initRedis() {
if (!AppConst.USE_REDIS) return;
if (!AppConst.USE_REDIS_CACHE) return;
pool = new JedisPool(AppConst.REDIS_IP, AppConst.REDIS_PORT);
client = pool.getResource();
}

public Object get(String dbname, String uid, String key) {
return AppConst.USE_REDIS ? getInner(dbname, uid, key) : null;
public Object get(String tbname, String uid, String key) {
return AppConst.USE_REDIS_CACHE ? getInner(tbname, uid, key) : null;
}

private Object getInner(String dbname, String uid, String key) {
return null;
private Object getInner(String tbname, String uid, String key) {
String strKey = tbname + "_" + uid + "_" + key;
return client.get(strKey);
}

public void set(String tbname, String uid, String key, Object value) {
if (!AppConst.USE_REDIS) return;
if (!AppConst.USE_REDIS_CACHE) return;
setInner(tbname, uid, key, value);
}

private void setInner(String tbname, String uid, String key, Object value) {
String strKey = tbname + "_" + uid + "_" + key;
client.set(strKey, value.toString());
}

public void remove(String tbname, String uid, String key) {
String strKey = tbname + "_" + uid + "_" + key;
client.del(strKey);
}

public void closeRedis() {
if (!AppConst.USE_REDIS) return;
if (!AppConst.USE_REDIS_CACHE) return;
client.close();
pool.close();
}
}

0 comments on commit 5013932

Please sign in to comment.