Redis中的深层嵌套数据结构

时间:2019-01-21 00:17:25

标签: redis

例如,我有对象数组

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KeyLogger implements NativeKeyListener {

    private static final Path file = Paths.get("keys.txt");
    private static final Logger logger = LoggerFactory.getLogger(KeyLogger.class);

    public static void main(String[] args) {

        logger.info("Key logger has been started");

        init();

        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException e) {
            logger.error(e.getMessage(), e);
            System.exit(-1);
        }

        GlobalScreen.addNativeKeyListener(new KeyLogger());
    }

    private static void init() {

        // Get the logger for "org.jnativehook" and set the level to warning.
        java.util.logging.Logger logger = java.util.logging.Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.WARNING);

        // Don't forget to disable the parent handlers.
        logger.setUseParentHandlers(false);
    }

    public void nativeKeyPressed(NativeKeyEvent e) {
                System.out.println("nativeKeyPressed -> getKeyCode " + e.getKeyCode());
                System.out.println("nativeKeyPressed -> getKeyChar " + e.getKeyChar());
                System.out.println("nativeKeyPressed -> getRawCode " + e.getRawCode());
    }

    public void nativeKeyReleased(NativeKeyEvent e) {
                System.out.println("nativeKeyReleased -> getKeyCode " + e.getKeyCode());
                System.out.println("nativeKeyReleased -> getKeyChar " + e.getKeyChar());
                System.out.println("nativeKeyReleased -> getRawCode " + e.getRawCode());
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
                System.out.println("nativeKeyTyped -> getKeyCode " + e.getKeyCode());
                System.out.println("nativeKeyTyped -> getKeyChar " + e.getKeyChar());
                System.out.println("nativeKeyTyped -> getRawCode " + e.getRawCode());
    }
}

将其存储在Redis缓存中的最佳方法是什么?我想在每次发送消息时进行更新。

1 个答案:

答案 0 :(得分:0)

有多种方法可以在Redis中存储此信息。

  • 制作对象的jsonString并存储简单的键/值对。

    • 优点:很好的主动性,在这种情况下json解析似乎很快。
  • 使用HashMap将jsonObject存储在Redis中。

    • 优点:无需解析JSON对象。
    • 缺点:带有对象的对象无法存储。

对于用例,请选择选项1。