Game Cache Rough Talk

Source: Internet
Author: User
Tags crc32

1. Local cache and distributed cache

game server in order to ensure the performance of access data need to cache the player's data, depending on the location of the cache, can be divided into: local JVM cache and distributed cache. The previous 2 games were 2 ways to get in touch with each other.

local JVM Cache: There are many implementations, and a better way is to use some third-party caches, such as Ehcache, to provide a variety of caching strategies, or to implement a local cache yourself, such as through Concurrenthashmap.

Distributed cache: The server-side implementation cluster is also supported after the previous contact redis,redis3.0 .
(more http://my.oschina.net/OutOfMemory/blog/412408)

2. Caching and Database

Some games may put the cache tightly as a cache, get when the first from the data and then put into the cache, the real need to update,delete is the direct operation of the database, this way I think is a more eclectic way, not only to ensure a certain performance, there is guaranteed security.

Another situation is that everything is done in the cache and then synchronized to the database on a regular basis, and there is a risk that the cached data will not be synchronized to the database. Of course, if you use a redis-like cache, it itself provides 2 kinds of persistent functions, namely, RDB and aof, in fact, the RDB mode can also be lost data.

Game companies feel more focused on performance, sometimes the data security this piece is not special attention, is able to tolerate data loss (game back), anyway, I am not particularly in favor of this, but the fact is that a lot of companies are doing so.

3. Data Synchronization

Local cache synchronization saw the implementation of 2 ways, one is to put all the additions and deletions into the queue sequentially, and then use the timer to read the queue, data synchronization to the database; the other is to check whether the object changes by CRC32 and then synchronize.

Let's take a look at an example of CRC32

public class Crc32util {/** * Gets the CRC value of the object * * @param object * The object that implements the serializable interface * @return */public static long Getcrc (Object object) {long CRC = 0; CRC32 CRC32 = new CRC32 (); Crc32.update (Object2byte (object)); CRC = Crc32.getvalue (); return CRC;} /** * Tell the Serializable object to a byte array * * @param object * Implements serializable interface objects * @return */public static byte[] Object2byte (objec T object) {byte data[] = Null;objectoutputstream out = null; Bytearrayoutputstream BAOs = null;try {BAOs = new Bytearrayoutputstream (); out = new ObjectOutputStream (BAOs); o Ut.writeobject (object);d ATA = Baos.tobytearray ();} catch (Exception e) {e.printstacktrace ();} finally {if (out! = null) {try {out.close ();} catch (IOException e) {E.printsta Cktrace ();}} if (BAOs! = null) {try {baos.close ();} catch (IOException e) {e.printstacktrace ();}}} return data;}} 
public class Bagcache implements Serializable {private static final long Serialversionuid = 1l;private long id;private int pid;private int Num;public Bagcache (long id, int pid, int num) {this.id = Id;this.pid = Pid;this.num = num;}        Get.set Method}
public class Rolecache implements Serializable {private static final long serialversionuid = 1l;private int roleid;private String rolename;private map<long, bagcache> bagmap = new Hashmap<long, bagcache> ();p ublic RoleCache (int role Id, String roleName) {This.roleid = Roleid;this.rolename = RoleName;} Public Bagcache getbag (long id) {return bagmap.get (ID);} public void Putbag (Bagcache bag) {bagmap.put (Bag.getid (), bag);}        Get.set Method}
public class Crc32test {public static void main (string[] args) {rolecache role = new Rolecache (1, "A1"); Bagcache bag = new Bagcache (1, 1, 1); Role.putbag (bag); long CRC = CRC32UTIL.GETCRC (role); System.out.println ("First time:" + CRC); Role.setrolename ("A2"); CRC = CRC32UTIL.GETCRC (role); System.out.println ("Second time:" + CRC); Bag.setnum (2); CRC = CRC32UTIL.GETCRC (role); System.out.println ("Third time:" + CRC);}}

Determine whether the current object needs to be synchronized by periodically checking the value of the CRC32 of the serializable memory object.

About distributed cache, because itself is to support persistence, in many cases do not need to do the persistence, such as MOGODB, of course, there is a problem is like Redis and Mogodb cache database, is not particularly convenient for us to do statistics, so we also often use Redis +mysql, the data is also synchronized to MySQL, on the one hand is for security, on the one hand is convenient to do data statistics. Redis synchronization to MySQL thinking is similar, the key additions and deletions into the Redis set list, and then by the timer to read the set list, synchronization MySQL.

Game Cache Rough Talk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.