-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
75 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
common/src/main/java/cn/zbx1425/worldcomment/data/sync/RedisCommand.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
common/src/main/java/cn/zbx1425/worldcomment/data/sync/RedisMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cn.zbx1425.worldcomment.data.sync; | ||
|
||
import cn.zbx1425.worldcomment.data.CommentEntry; | ||
import cn.zbx1425.worldcomment.data.ServerWorldData; | ||
import io.lettuce.core.api.StatefulRedisConnection; | ||
|
||
public class RedisMessage { | ||
|
||
public static final String COMMAND_CHANNEL = "WORLD_COMMENT_COMMAND_CHANNEL"; | ||
|
||
private static final String INSTANCE_ID = Long.toHexString(ServerWorldData.SNOWFLAKE.nextId()); | ||
|
||
public String initiator; | ||
public String action; | ||
public String content; | ||
|
||
public RedisMessage(String action, String content) { | ||
this.initiator = INSTANCE_ID; | ||
this.action = action; | ||
this.content = content; | ||
} | ||
|
||
public RedisMessage(String redisCommand) { | ||
int firstHash = redisCommand.indexOf(':'); | ||
int lastHash = redisCommand.lastIndexOf(':'); | ||
this.action = redisCommand.substring(0, firstHash); | ||
this.initiator = redisCommand.substring(firstHash + 1, lastHash); | ||
this.content = redisCommand.substring(lastHash + 1); | ||
} | ||
|
||
public static RedisMessage insert(CommentEntry entry) { | ||
return new RedisMessage("INSERT", entry.toBinaryString()); | ||
} | ||
|
||
public static RedisMessage update(CommentEntry entry) { | ||
return new RedisMessage("UPDATE", entry.toBinaryString()); | ||
} | ||
|
||
public void publishAsync(StatefulRedisConnection<String, String> connection) { | ||
connection.async().publish(COMMAND_CHANNEL, String.format("%s:%s:%s", action, initiator, content)); | ||
} | ||
|
||
public boolean isFromSelf() { | ||
return initiator.equals(INSTANCE_ID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters