/** * Write single entry to the cache and to the database. * Input: * key = key with prefix * val = value string * Output: * entry written to database * cache updated */ private void WriteCache(string key, string val) { // write to database using (MySqlCommand cmd = getCommand()) { string command = "INSERT INTO ScriptDBs SET sdb_key=?sdbkey,sdb_val=?sdbval"; command += " ON DUPLICATE KEY UPDATE sdb_val=?sdbval"; cmd.CommandText = command; cmd.Parameters.AddWithValue("?sdbkey", key); cmd.Parameters.AddWithValue("?sdbval", val); cmd.ExecuteNonQuery(); } // remove from cache wherever it is // make a new entry if not there already ScriptDBCacheEntry entry; if (m_Engine.m_ScriptDBCacheEntries.TryGetValue(key, out entry)) { m_Engine.RemFromScriptDBCache(entry); } else { entry = new ScriptDBCacheEntry(); entry.sdbkey = key; } // update cache value entry.sdbval = val; // re-insert into cache as newest entry m_Engine.AddToScriptDBCache(entry); }
/** * Read single element from database. * Input: * key = as given to xmrScriptDBWrite() * notfound = value to return if not found * Output: * returns notfound: record not found * else: value as given to xmrScriptDBWrite() */ public override string xmrScriptDBReadOne(string key, string notfound) { lock (m_Engine.m_ScriptDBLock) { ScriptDBCacheEntry entry = ReadCache(getPrefix() + key); return((entry.sdbval == null) ? notfound : entry.sdbval); } }
/** * Read single entry from the cache (and database if not in cache). * Input: * key = key with prefix * Output: * returns cache entry (sdbval == null if record not found) */ private ScriptDBCacheEntry ReadCache(string key) { // remove from cache wherever it is // create new entry if we don't already have one ScriptDBCacheEntry entry; if (m_Engine.m_ScriptDBCacheEntries.TryGetValue(key, out entry)) { //?? optimization ??// if (entry.loaded == m_Engine.m_ScriptDBCacheSeq) return entry; m_Engine.RemFromScriptDBCache(entry); } else { entry = new ScriptDBCacheEntry(); entry.sdbkey = key; } // make sure it was created at or after when this script was instanced so we know data isn't stale if (entry.loaded < m_Instanced) { using (MySqlCommand cmd = getCommand()) { cmd.CommandText = "SELECT sdb_val FROM ScriptDBs WHERE sdb_key=?sdbkey"; cmd.Parameters.AddWithValue("?sdbkey", key); using (IDataReader rdr = cmd.ExecuteReader()) { entry.sdbval = rdr.Read() ? rdr["sdb_val"].ToString() : null; } } } // add (back to) cache as the newest entry m_Engine.AddToScriptDBCache(entry); return(entry); }