Cache Playerdata in Hashmap

Discussion in 'Programming' started by CyCode, Jul 30, 2020.

  1. Hey, I am currently working on my first Plugin with Database integration. Now I have a question that cam into my mind. Probably its pretty stupid but I ask it anyway. I have my Database Integration set and ready, and it runs pretty well. Now I want to cache the data of the Player, so I don't have to contact the Database every time I want the information from the Player.
    Now the stupid question: If I store the Data of the Player in a Hashmap will the Data I set only exist for the Player or will the Data exist for everyone?
    Example: The user joins, and the Plugin contacts the Database, for his information. Then I want to return the Rank of the Player. If I save the Data now in the Hasmap. And another user joins. Will the Data be overridden? Do I have to store the UUID for the Player too if this is the case?
  2. Strahan


    It all depends how you design it. If you use the UUID as the key, then no, it won't overwrite anyone else's data as their UUIDs will be different.
    • Winner Winner x 1
  3. Like said above, the data will be overridden only if 2 players have the same key in your map.
    UUID stands for "Universal unique identifier" so you probably guess that it shouldn't happen. Meaning that you can (and should) use it as the key
    • Winner Winner x 1
  4. Thank you guys. I actually forgot, that I can use multiple Hashmaps with the UUID as key wow. I tried so long to save this shit into one HashMap.
  5. Just in case this is what you planned to do, don't create a map for each stats (money, level, rank, whatever)
    You can create a "PlayerStats" class which contain all of that and then a Map<Player,PlayerStats>
    • Agree Agree x 1
  6. Oh, thank you didn't think of that. I will try it :)