Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 9885:64e16d1e91f6
mod_storage_internal,_sql: Key item count cache on both username and store
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 22 Mar 2019 18:02:27 +0100 |
parent | 9884:9751c17f5281 |
child | 9889:e3ad2c845431 |
comparison
equal
deleted
inserted
replaced
9884:9751c17f5281 | 9885:64e16d1e91f6 |
---|---|
5 local json = require "util.json"; | 5 local json = require "util.json"; |
6 local sql = require "util.sql"; | 6 local sql = require "util.sql"; |
7 local xml_parse = require "util.xml".parse; | 7 local xml_parse = require "util.xml".parse; |
8 local uuid = require "util.uuid"; | 8 local uuid = require "util.uuid"; |
9 local resolve_relative_path = require "util.paths".resolve_relative_path; | 9 local resolve_relative_path = require "util.paths".resolve_relative_path; |
10 local jid_join = require "util.jid".join; | |
10 | 11 |
11 local is_stanza = require"util.stanza".is_stanza; | 12 local is_stanza = require"util.stanza".is_stanza; |
12 local t_concat = table.concat; | 13 local t_concat = table.concat; |
13 | 14 |
14 local noop = function() end | 15 local noop = function() end |
235 quota = archive_item_limit; | 236 quota = archive_item_limit; |
236 truncate = true; | 237 truncate = true; |
237 }; | 238 }; |
238 archive_store.__index = archive_store | 239 archive_store.__index = archive_store |
239 function archive_store:append(username, key, value, when, with) | 240 function archive_store:append(username, key, value, when, with) |
240 local item_count = archive_item_count_cache:get(username); | 241 local cache_key = jid_join(username, host, self.store); |
242 local item_count = archive_item_count_cache:get(cache_key); | |
241 if not item_count then | 243 if not item_count then |
242 local ok, ret = engine:transaction(function() | 244 local ok, ret = engine:transaction(function() |
243 local count_sql = [[ | 245 local count_sql = [[ |
244 SELECT COUNT(*) FROM "prosodyarchive" | 246 SELECT COUNT(*) FROM "prosodyarchive" |
245 WHERE "host"=? AND "user"=? AND "store"=?; | 247 WHERE "host"=? AND "user"=? AND "store"=?; |
253 end); | 255 end); |
254 if not ok or not item_count then | 256 if not ok or not item_count then |
255 module:log("error", "Failed while checking quota for %s: %s", username, ret); | 257 module:log("error", "Failed while checking quota for %s: %s", username, ret); |
256 return nil, "Failure while checking quota"; | 258 return nil, "Failure while checking quota"; |
257 end | 259 end |
258 archive_item_count_cache:set(username, item_count); | 260 archive_item_count_cache:set(cache_key, item_count); |
259 end | 261 end |
260 | 262 |
261 module:log("debug", "%s has %d items out of %d limit", username, item_count, archive_item_limit); | 263 module:log("debug", "%s has %d items out of %d limit", username, item_count, archive_item_limit); |
262 if item_count >= archive_item_limit then | 264 if item_count >= archive_item_limit then |
263 return nil, "quota-limit"; | 265 return nil, "quota-limit"; |
278 ]]; | 280 ]]; |
279 if key then | 281 if key then |
280 local result, err = engine:delete(delete_sql, host, user or "", store, key); | 282 local result, err = engine:delete(delete_sql, host, user or "", store, key); |
281 if result then | 283 if result then |
282 item_count = item_count - result:affected(); | 284 item_count = item_count - result:affected(); |
283 archive_item_count_cache:set(username, item_count); | 285 archive_item_count_cache:set(cache_key, item_count); |
284 end | 286 end |
285 else | 287 else |
286 item_count = item_count + 1; | 288 item_count = item_count + 1; |
287 key = uuid.generate(); | 289 key = uuid.generate(); |
288 end | 290 end |
289 local t, encoded_value = assert(serialize(value)); | 291 local t, encoded_value = assert(serialize(value)); |
290 engine:insert(insert_sql, host, user or "", store, when, with, key, t, encoded_value); | 292 engine:insert(insert_sql, host, user or "", store, when, with, key, t, encoded_value); |
291 archive_item_count_cache:set(username, item_count+1); | 293 archive_item_count_cache:set(cache_key, item_count+1); |
292 return key; | 294 return key; |
293 end); | 295 end); |
294 if not ok then return ok, ret; end | 296 if not ok then return ok, ret; end |
295 return ret; -- the key | 297 return ret; -- the key |
296 end | 298 end |
458 sql_query = string.format(sql_query, t_concat(where, " AND "), | 460 sql_query = string.format(sql_query, t_concat(where, " AND "), |
459 query.reverse and "ASC" or "DESC", unlimited); | 461 query.reverse and "ASC" or "DESC", unlimited); |
460 end | 462 end |
461 return engine:delete(sql_query, unpack(args)); | 463 return engine:delete(sql_query, unpack(args)); |
462 end); | 464 end); |
463 archive_item_count_cache:set(username, nil); | 465 local cache_key = jid_join(username, host, self.store); |
466 archive_item_count_cache:set(cache_key, nil); | |
464 return ok and stmt:affected(), stmt; | 467 return ok and stmt:affected(), stmt; |
465 end | 468 end |
466 | 469 |
467 local stores = { | 470 local stores = { |
468 keyval = keyval_store; | 471 keyval = keyval_store; |