Skip to content

Commit

Permalink
src/model: Added caching for Players and Missions
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Bajolet <[email protected]>
  • Loading branch information
lbajolet committed Dec 6, 2016
1 parent 3d2b8ba commit 39d1390
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/model/missions.nit
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module missions

import model::players

redef class DBContext

# Cache for missions by their ID
#
# Used for fast fetching and shared instances
var mission_cache = new HashMap[Int, Mission]

fun mission_worker: MissionWorker do return once new MissionWorker

fun mission_status_worker: MissionStatusWorker do return once new MissionStatusWorker

fun mission_by_id(id: Int): nullable Mission do return mission_worker.fetch_one(self, "* FROM missions WHERE id = {id};")
fun mission_by_id(id: Int): nullable Mission do
if mission_cache.has_key(id) then return mission_cache[id]
var m = mission_worker.fetch_one(self, "* FROM missions WHERE id = {id};")
if m != null then mission_cache[id] = m
return m
end

fun mission_by_slug(slug: String): nullable Mission do return mission_worker.fetch_one(self, "* FROM missions WHERE slug = {slug.to_sql_string};")

Expand Down Expand Up @@ -51,6 +60,7 @@ class MissionWorker
redef fun make_entity_from_row(ctx, row) do
var map = row.map
var id = map["id"].as(Int)
if ctx.mission_cache.has_key(id) then return ctx.mission_cache[id]
var slug = map["slug"].as(String)
var title = map["title"].as(String)
var tid = map["track_id"].as(Int)
Expand All @@ -61,6 +71,7 @@ class MissionWorker
ret.id = id
ret.solve_reward = rew
ret.load_languages
ctx.mission_cache[id] = ret
return ret
end
end
Expand Down Expand Up @@ -178,7 +189,9 @@ class Mission
redef fun insert do
var p = path
if p != null then p = p.to_sql_string
return basic_insert("INSERT INTO missions(slug, title, track_id, description, reward, path) VALUES({slug.to_sql_string}, {title.to_sql_string}, {track_id}, {desc.to_sql_string}, {solve_reward}, {p or else "NULL"});") and set_dependencies
var ret = basic_insert("INSERT INTO missions(slug, title, track_id, description, reward, path) VALUES({slug.to_sql_string}, {title.to_sql_string}, {track_id}, {desc.to_sql_string}, {solve_reward}, {p or else "NULL"});") and set_dependencies
if ret then context.mission_cache[id] = self
return ret
end

redef fun update do
Expand Down
15 changes: 13 additions & 2 deletions src/model/players.nit
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ import model::model_base

redef class DBContext

var player_cache = new HashMap[Int, Player]

fun player_worker: PlayerWorker do return once new PlayerWorker

# Tries to find a player item from its `id`
fun player_by_id(id: Int): nullable Player do return player_worker.fetch_one(self, "* FROM players WHERE id = {id};")
fun player_by_id(id: Int): nullable Player do
if player_cache.has_key(id) then return player_cache[id]
var p = player_worker.fetch_one(self, "* FROM players WHERE id = {id};")
if p != null then player_cache[id] = p
return p
end

# Gets the `limit` top players by score
fun all_players: Array[Player] do return player_worker.fetch_multiple(self, "* FROM players;")
Expand All @@ -42,6 +49,7 @@ class PlayerWorker
redef fun make_entity_from_row(ctx, row) do
var m = row.map
var id = m["id"].as(Int)
if ctx.player_cache.has_key(id) then return ctx.player_cache[id]
var slug = m["slug"].as(String)
var name = m["name"].as(String)
var email = m["email"].as(String)
Expand All @@ -50,6 +58,7 @@ class PlayerWorker
var p = new Player(ctx, slug, name, email, avatar_url)
p.id = id
p.date_joined = date
ctx.player_cache[id] = p
return p
end
end
Expand Down Expand Up @@ -87,7 +96,9 @@ class Player

redef fun insert do
if date_joined == -1 then date_joined = get_time
return basic_insert("INSERT INTO players(slug, name, date_joined, email, avatar_url) VALUES({slug.to_sql_string}, {name.to_sql_string}, {date_joined}, {email.to_sql_string}, {avatar_url.to_sql_string});")
var ret = basic_insert("INSERT INTO players(slug, name, date_joined, email, avatar_url) VALUES({slug.to_sql_string}, {name.to_sql_string}, {date_joined}, {email.to_sql_string}, {avatar_url.to_sql_string});")
if ret then context.player_cache[id] = self
return ret
end

redef fun update do return basic_update("UPDATE players SET name = {name.to_sql_string}, email = {email.to_sql_string}, avatar_url = {avatar_url.to_sql_string} WHERE id = {id};")
Expand Down

0 comments on commit 39d1390

Please sign in to comment.