how to make the String readable in redis?

34 views Asked by At
public class RedisStorage {

    int usersPerSeconds = 0;

    int generateUserPaid;

    int totalUsersPerSecond = 20;
    private RedissonClient redissonClient ;
    private RKeys rKeys;

    private RScoredSortedSet<String> registeredUsers;

    private double getTs()
    {
        return new Date().getTime()/1000;
    }

    public void listKeys()
    {
        Iterable<String> keys = rKeys.getKeys();
        for(String key : keys)
        {
            System.out.println("Key: "+key + ", type:" +rKeys.getType(key));
        }
    }

    public void run() throws InterruptedException {
        init();
        displayUsers();
    }

    public void init()
    {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        try {
            redissonClient = Redisson.create(config);
        }catch (RedisConnectionException Rdc)
        {
            System.out.println("Connection Failed");
            System.out.println(Rdc.getMessage());
        }

        rKeys = redissonClient.getKeys();
        registeredUsers = redissonClient.getScoredSortedSet("REGISTERED_USERS");
        rKeys.delete("REGISTERED_USERS");
    }

    void shutDown()
    {
        redissonClient.shutdown();
    }


    void deleteOldUsers(int secondsAgo)
    {
        registeredUsers.removeRangeByScore(0,true,getTs()-secondsAgo,true);
    }

    int calculateUsers()
    {
        return   registeredUsers.count(Double.NEGATIVE_INFINITY,true,Double.POSITIVE_INFINITY,true);
    }

    void displayUsers() throws InterruptedException
    {
        while (true) {

            deleteOldUsers(usersPerSeconds);
            for (int i = 1; i <= 20; i++) {

                usersPerSeconds++;

                generateUserPaid = new Random().nextInt(totalUsersPerSecond) + 1;

                boolean checkUserPayment = !(generateUserPaid == usersPerSeconds);

                if (i == new Random().nextInt(totalUsersPerSecond) + 1 && checkUserPayment) {

                    System.out.println("User  " + generateUserPaid + " paid for the service");

                    registeredUsers.add(getTs(), "User " + String.valueOf(generateUserPaid) +" paid for the service");

                }

                Thread.sleep(500);

                System.out.println("Displaying user " + usersPerSeconds);

                registeredUsers.add(getTs(), "Displaying User " + String.valueOf(usersPerSeconds));

                if (usersPerSeconds == 20) {

                    usersPerSeconds = 0;

                }

            }
            Thread.sleep(1000);

        }
    }
}

The result I get in redis console

redis-cli --raw

♥Displaying User 1│
1703687562
♥Displaying User 1┤
1703687562
♥User 2 paid for the servicх
1703687562
♥Displaying User 1╡
1703687563
♥Displaying User 1╢
1703687563
♥Displaying User 1╖
1703687564
♥Displaying User 1╕
1703687564
♥Displaying User 1╣
1703687565
♥Displaying User 2░

The result in java console which it should be equivalent in redis console

Displaying user 1
User  4 paid for the service
Displaying user 2
Displaying user 3
Displaying user 1
Displaying user 2
User  20 paid for the service
Displaying user 3
Displaying user 4
Displaying user 5
Displaying user 13
Displaying user 14
Displaying user 15
Displaying user 16

To avoid confusion we are here focusing on how to make the characters readable in redis console.

0

There are 0 answers