Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Caching db query results

Comments in 'Plugin Development' started by Legoboy0215, Sep 16, 2016.

  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    The only question I have here: Is querying a SQLite database every second for 20 players that bad? I currently cache the values in an array, which is my idea of saving resources. Any ideas?
    Muqsit likes this.
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Using Async worker, no.
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Sorry, I meant like on the main thread. An async worker would not affect the main thread obviously :p
  4. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    what exactly do you want to chache what data are you using... Please tell us your situation.
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Cache kill stats.
  6. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Try to work with a session array. When a player joins query his kill/deaths and store it by his username in an array. Update it on every kill or death and upload the array content when the player leaves.
    Gamecrafter likes this.
  7. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Caching is definitely the way to go. Why would you query every second? That's just an overload on resources. And don't do it on the main thread, 20 players/second + querying a database = tons of lag on your server.
    Caching kill stats? Like when a player is killed? Easy. Create a session that first queries the database when the user joins. Work from this session, and your stats will be updated in the session only. After the player leaves, then update the database, with the information from the session. Then destroy the session. But what happens if the user joined, didn't do anything, and quit? Add an extra value to your session, one that indicates whether or not anything has been changed. If this value is true (or whatever you decide to use to indicate something changed when the player was online), push these updates to the database. But if nothing change, just destroy the session and don't do anything with the database, because nothing changed.
    An array of session objects sounds like the way to go. @PEMapModder did this for LegionPE's plugin. LBSG also does this. Object-oriented programming ftw! :D
    Legoboy0215 and applqpak like this.
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Oh. So what I did, creating a session object and caching all the stats was right :)
    No, I was actually wondering is it THAT bad, but it obviously is now :D
  9. EntirelyQuartz
    Offline

    EntirelyQuartz New Member

    Joined:
    Aug 9, 2016
    Posts:
    15
    Minecraft User:
    EntirelyQuartz
    Why you need query each second? That's totally useless.

    Instead you can save it when the player leave the server or, if you need the data instantly, you can just update it when the player death or kill someone.
  10. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    The only problem with updating the database from a cache when a player leaves is that the database won't be updated if the server crashes or segfaults.
    Legoboy0215 and applqpak like this.
  11. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    That is the point. I never said I actually queried the database every second, since it is obvious that caching is better. But I wanted to know how much better?
    Yeah. But it is worth it since the servers don't normally crash randomly.

Share This Page

Advertisement