Skip to content

Commit

Permalink
updt with built-in cache support.
Browse files Browse the repository at this point in the history
  • Loading branch information
wadelau committed Aug 3, 2018
1 parent 9b60f45 commit e24e16a
Show file tree
Hide file tree
Showing 11 changed files with 1,801 additions and 276 deletions.
110 changes: 100 additions & 10 deletions java/inc/Cachea.class.jsp
Expand Up @@ -5,25 +5,115 @@
* since Wed Jul 13 18:22:06 UTC 2011
* Thu Sep 11 16:34:20 CST 2014
* Ported into Java by wadelau@ufqi.com, June 28, 2016s
* with various connection and/or different drivers support
*/
%><%@include file="./Memcached.class.jsp"%><%
%><%!
public final class Cachea { //- cache administrator
private HashMap myConf;
//- variables
private final static int MAX_CONNECTION = 5;
private CacheConn myConn;
private CacheDriver myDriver;
private int myExpire = 30 * 60;
private HashMap myConn;
//-
//- @todo
public Cachea(String conf){
//- @todo
//- constructor
public Cachea(String hmconf){
hmconf = hmconf==null ? "" : hmconf;
hmconf = hmconf.equals("") ? "Cache_Master" : hmconf;
this.myConn = new CacheConn(hmconf);
this.myExpire = this.myConn.myExpire;
String strDriver = (String)Config.get("cachedriver");
if(strDriver.equals("MEMCACHED")){
this.myDriver = new Memcached(this.myConn);
//debug("inc/Cachea: drv:"+myDriver+" is initiated by conn:"+myConn);
}
else{
System.out.println("inc/Cachea: Unsupported cacheDriver:["
+strDriver+"]. 1808012202.");
}
}
//- methods
public HashMap get(String key){
Object myobj = this.myDriver.get(key);
if(myobj != null){
return (new HashMap(){{
put(0, true);
put(1, myobj);
}});
}
else{
return (new HashMap(){{
put(0, false);
put(1, "error:"+myobj);
}});
}
}
//-
public HashMap set(String key, Object value){
return this.set(key, value, this.myExpire);
}
//-
public HashMap set(String key, Object value, int seconds){
boolean issucc = this.myDriver.set(key, value, seconds);
if(issucc){
return (new HashMap(){{
put(0, true);
put(1, issucc);
}});
}
else{
return (new HashMap(){{
put(0, false);
put(1, "error:"+issucc);
}});
}
}
//-
public HashMap rm(String key){
boolean issucc = this.myDriver.rm(key);
if(issucc){
return (new HashMap(){{
put(0, true);
put(1, issucc);
}});
}
else{
return (new HashMap(){{
put(0, false);
put(1, "error:"+issucc);
}});
}
}
//-
}
%>
//----
//- interface of CacheDriver
public interface CacheDriver{
//- @todo in Impls classes
//private void _init();
public Object get(String key);
public boolean set(String key, Object value);
public boolean set(String key, Object value, int seconds);
public boolean rm(String key);
}
%>
41 changes: 16 additions & 25 deletions java/inc/Config.class.jsp
Expand Up @@ -7,6 +7,7 @@
#
*/
//- exec
if(true){
Expand All @@ -17,43 +18,32 @@ if(true){
hmconf.put("dbhost", "localhost");
hmconf.put("dbport", 3306);
hmconf.put("dbuser", "");
hmconf.put("dbpassword", "");
hmconf.put("dbname", "");
hmconf.put("dbdriver", "MYSQL"); //- MYSQL, SQLSERVER, ORACLE, INFORMIX, SYBASE
hmconf.put("db_enable_utf8_affirm", false);
hmconf.put("db_enable_socket_pool", true); //- Fri Jul 20 13:21:41 UTC 2018
/*
hmconf.put("dbhost", "192.168.0.241");
hmconf.put("dbport", 3306);
hmconf.put("dbuser", "");
hmconf.put("dbpassword", "");
hmconf.put("dbname", "");
hmconf.put("dbdriver", "MYSQL"); //- MYSQL, SQLSERVER, ORACLE, INFORMIX, SYBASE
hmconf.put("db_enable_utf8_affirm", false);
*/
//- cache
hmconf.put("enable_cache", false);
hmconf.put("cachehost", "");
hmconf.put("cacheport", "");
hmconf.put("cachedriver", "MEMCACHEX"); //- REDISX, XCACHEX
hmconf.put("enable_cache", true);
hmconf.put("cachehost", "localhost");
hmconf.put("cacheport", 8800);
hmconf.put("cachedriver", "MEMCACHED"); //- REDISX, XCACHEX
hmconf.put("cacheexpire", 30*60);
hmconf.put("cachemaxconn", 6); //- max connections for pool
//- session
hmconf.put("enable_session", true);
hmconf.put("sessionhost", "");
hmconf.put("sessionport", "");
hmconf.put("sessiondriver", "SESSIONX"); //- SESSIONX
hmconf.put("sessionexpire", 30*60);
//- session
hmconf.put("enable_session", true);
hmconf.put("sessionhost", "localhost");
hmconf.put("sessionport", 9900);
hmconf.put("sessiondriver", "SESSIONX"); //- SESSIONX
hmconf.put("sessionexpire", 30*60);
hmconf.put("sign_key", "--my sign key--");
hmconf.put("sign_key", "--Mon Jul 2 12:55:44 UTC 2018##");
//- tpl
hmconf.put("template_display_index", true); //- true for embedded, false for standalone
//- init config
Config.setConf(hmconf);
Expand All @@ -63,7 +53,8 @@ if(true){
//- define
public final static class Config {
//public final static class Config {
public static class Config {
private static HashMap conf = new HashMap();
Expand Down Expand Up @@ -108,4 +99,4 @@ public final static class Config {
//- where to initialize in a app-global container?
%>
%>
28 changes: 26 additions & 2 deletions java/inc/Conn.class.jsp
Expand Up @@ -39,6 +39,14 @@ public class DbConn{
//- slave
}
else if(dbServer.equals("userdb")){
this.myHost = (String)Config.get("dbhost_userdb"); //- inc/Config.class
this.myPort = (Integer)Config.get("dbport_userdb");
this.myUser = (String)Config.get("dbuser_userdb");
this.myPwd = (String)Config.get("dbpassword_userdb");
this.myDb = (String)Config.get("dbname_userdb");
this.myDriver = (String)Config.get("dbdriver_userdb");
}
else{
System.out.println("Unknown dbServer:["+dbServer+"]. 1607021811.");
}
Expand All @@ -50,9 +58,25 @@ public class DbConn{
//-
public static class CacheConn{
String myHost = "";
int myPort = 0;
String myDriver = "";
int myExpire = 30*60;
int myMaxConn = 5;
//- @todo, socket pool, config, connection
public CacheConn(String cacheServer){
//- @todo
cacheServer = cacheServer==null ? "" : cacheServer;
cacheServer = cacheServer.equals("") ? "Cache_Master" : cacheServer;
if(cacheServer.equals("Cache_Master")){
this.myHost = (String)Config.get("cachehost");
this.myPort = (Integer)Config.get("cacheport");
this.myDriver = (String)Config.get("cachedriver");
this.myExpire = (Integer)Config.get("cacheexpire");
this.myMaxConn = (Integer)Config.get("cachemaxconn");
}
else{
debug("inc/Conn: unsupported cacheServer:["+cacheServer+"] 1808012241.");
}
}
}
Expand All @@ -68,4 +92,4 @@ public static class SessionConn{
}
%>
%>

0 comments on commit e24e16a

Please sign in to comment.