Skip to content

Instantly share code, notes, and snippets.

@Goblom
Created February 19, 2014 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Goblom/9100329 to your computer and use it in GitHub Desktop.
Save Goblom/9100329 to your computer and use it in GitHub Desktop.
Caching System
/*
* Copyright 2014 Goblom.
*/
import java.sql.SQLException;
/**
*
* @author Goblom
*/
public abstract class CachedTable {
protected final String tableName;
public CachedTable(String tableName) {
this.tableName = tableName;
}
/*
* Create Necessary columns and values for this table
*/
public abstract void createTable();
/*
* Used to Gather all data from the table and cycle through and cach each row.
*/
public abstract void update() throws SQLException;
/*
* Cycle through each Row and get the row with the given id
*/
public abstract <T extends CachedRow> T getRow(int id);
public abstract class CachedRow {
final int id;
public CachedRow(int id) {
this.id = id;
}
/*
* The rows ID. This should come from a colum named ID that is AUTO_INCEMENT
*/
public int getID() {
return id;
}
}
}
/*
* Copyright 2014 Goblom.
*/
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Goblom
*/
public class Example_CachedTable extends CachedTable {
private final List<Example_CachedRow> rows = new ArrayList();
public Example_CachedTable() {
super("example_cachedTable");
}
@Override
public void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS "
+ "example_cachedTable ("
+ "id int(11) NOT NULL AUTO_INCREMENT, "
+ "username varchar(16) NOT NULL, "
+ "online enum('true','false') NOT NULL, "
+ "PRIMARY KEY (`id`)"
+ ");";
// Get Threaded Querying System Here
// Send as update(sql);
}
@Override
public void update() throws SQLException {
this.rows.clear();
String sql = "SELECT * FROM `" + tableName + "'";
// Get Threaded Querying System Here
// Read it along the lines of this...
/*
getDatabase().query(sql, new Database.QueryHandler() {
@Override
public void onDataRecieve(DataRecieveEvent event) {
ResultSet rs = event.getResult();
if (rs != null) {
try {
rs.beforeFirst();
while (rs.next()) {
rows.add(
new Example_Row(
rs.getInt("id"),
rs.getString("username"),
Boolean.getBoolean(rs.getString("online"))
)
);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
});
*/
}
@Override
public Example_CachedRow getRow(int id) {
for (Example_CachedRow row : rows) {
if (row.getID() == id) {
return row;
}
}
return null;
}
public void newEntry(String username) {
int id = 0;
for (Example_CachedRow row : rows) {
if (row.getID() > 0) {
id = row.getID();
}
}
rows.add(new Example_CachedRow(id, username, true));
// SQL INSERT STATEMENT
// USING THE THREADED QUERYING SYSTEM
}
public class Example_CachedRow extends CachedTable.CachedRow {
private final String username;
private boolean online;
public Example_CachedRow(int id, String username, boolean online) {
super(id);
this.username = username;
this.online = online;
}
public String getUsername() {
return username;
}
public boolean getOnline() {
return online;
}
public void setOnline(boolean b) {
this.online = b;
}
synchronized void sendUpdate(String column, String value) {
// SQL UPDATE STATEMENT
// SEND AS update(sqlQuery);
}
}
}
/*
* Copyright 2014 Goblom.
*/
import java.sql.SQLException;
/**
*
* @author Goblom
*/
public class Example_CacheSystem {
private final Example_CachedTable example;
public Example_CacheSystem() throws SQLException {
/* Create Caches */
this.example = new Example_CachedTable();
/* Create Tables on Database */
this.example.createTable();
/* Load Data from the Database */
this.example.update();
}
public Example_CachedTable getExampleTable() {
return example;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment