# redis-mock **Repository Path**: KyleLuo/redis-mock ## Basic Information - **Project Name**: redis-mock - **Description**: 用Java模型实现的Redis Server,方便做集成测试的时候使用 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-04-20 - **Last Updated**: 2026-10-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Redis-server [![Build status](https://ci.appveyor.com/api/projects/status/fu71vlj9n9bixadg/branch/dev?svg=true)](https://ci.appveyor.com/project/lichangshu/redis-mock) Pure Java implementation redis-server. Embedded redis service when unit testing. You no longer need to Mock redis apis. 1. ~~Support redis api 2.8~~ 2. ~~jedis-3.0+ api is changed, so you must update it to 0.1.0-3.0 if you are using the latest api~~ 3. version 0.2.0 remove jedis dependence, There is no need for any external dependencies. 4. version 0.2.2 support `PubSubOperation` 5. version 0.2.3 ChannelInputStream to `ByteBuffer`, it is non blocking. 6. support RESP-3, to connect by `HELLO`, from release 0.3.0 7. support HyperLogLog from <0.3.1+> 8. support Lua-script from <0.3.3+> 9. Next BY AI 10. version **0.3.5** Geospatial (`GEO*`), and extended **Set / List / String / Hash** commands (see *Supported commands* below). ## maven dependency ``` com.github.microwww redis-server 0.3.6 test ``` ## Using ``` RedisServer server = new RedisServer(); server.listener("127.0.0.1", 6379); // Redis runs in the background InetSocketAddress address = (InetSocketAddress) server.getServerSocket().getLocalSocketAddress(); logger.info("Redis start :: [{}:{}]", address.getHostName(), address.getPort()); ``` > You can set port to 0, server will bind at **random port**, you can get it by `server.getLocalSocketAddress` if you are using spring boot, you can start like this : ``` @Bean public RedisServer mockRedisServer(RedisProperties redisProperties) throws IOException { RedisServer server = new RedisServer(); server.listener(redisProperties.getHost(), redisProperties.getPort()); InetSocketAddress address = (InetSocketAddress) server.getServerSocket().getLocalSocketAddress(); logger.info("Mocker Redis start :: [{}:{}], set 'server.redis.host' to match it", address.getHostName(), address.getPort()); return server; } ``` server will run with spring config : `spring.redis.host`, `spring.redis.port` A better option is to add dependencies: ``` com.github.microwww mocker-redis-spring-boot-starter 5.3.6 ``` ### Supported commands Supported redis commands : ConnectionOperation > AUTH, ECHO, PING, QUIT, SELECT, HELLO<0.3.0+>, HashOperation > HDEL, HEXISTS, HGET, HGETALL, HINCRBY, HINCRBYFLOAT, HKEYS, HLEN, HMGET, HMSET, HSCAN, HSET, HSETNX, HVALS, HRANDFIELD<0.3.5+>, HSTRLEN<0.3.5+>, GeoOperation `<0.3.5+>` > GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUSBYMEMBER, GEORADIUS_RO, GEORADIUSBYMEMBER_RO, HyperLogLog `<0.3.1+>` > PFADD, PFCOUNT, PFMERGE, KeyOperation > DEL, EXISTS, EXPIRE, EXPIREAT, KEYS, MOVE, PERSIST, PEXPIRE, PEXPIREAT, PTTL, RANDOMKEY, RENAME, RENAMENX, SCAN, SORT, TTL, TYPE, UNLINK<4.0.0+>, ListOperation > BLPOP, BRPOP, LINDEX, LINSERT, LLEN, LMOVE<0.3.5+>, LPOP, LPOS<0.3.5+>, LPUSH, LPUSHX, LRANGE, LREM, LSET, LTRIM, RPOP, RPOPLPUSH, RPUSH, RPUSHX, PubSubOperation `<0.2.2+>` > PSUBSCRIBE, PUBLISH, PUBSUB, PUNSUBSCRIBE, SUBSCRIBE, UNSUBSCRIBE, ScriptOperation `<0.3.3+>` > EVAL, EVALSHA, SCRIPT EXISTS, SCRIPT FLUSH, SCRIPT KILL, SCRIPT LOAD, ServerOperation > DBSIZE, FLUSHALL, FLUSHDB, TIME, 0.0.2+, CLIENT GETNAME, CLIENT ID<0.3.4+>, CLIENT KILL, CLIENT LIST, CLIENT SETNAME,CLIENT SETINFO<0.3.4+>, CLIENT INFO<0.3.4+>, SetOperation > SADD, SCARD, SDIFF, SDIFFSTORE, SINTER, SINTERCARD<0.3.5+>, SINTERSTORE, SISMEMBER, SMEMBERS, SMISMEMBER<0.3.5+>, SMOVE, SPOP, SRANDMEMBER, SREM, SSCAN, SUNION, SUNIONSTORE, SortedSetOperation > ZADD, ZCARD, ZCOUNT, ZINCRBY, ZINTERSTORE, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANK, ZSCAN, ZSCORE, ZUNIONSTORE, StringOperation > APPEND, BITCOUNT, BITOP, DECR, DECRBY, GET, GETBIT, GETDEL<0.3.5+>, GETEX<0.3.5+>, GETRANGE, GETSET, INCR, INCRBY, INCRBYFLOAT, MGET, MSET, MSETNX, PSETEX, SET, SETBIT, SETEX, SETNX, SETRANGE, STRLEN, SUBSTR<0.3.5+>, TransactionOperation > 0.0.2+, DISCARD, EXEC, MULTI, UNWATCH, WATCH ## Unsupported If you find unsupported operation, you can add it by yourself, you must `implements AbstractOperation` add it to server : > `server.configScheme(16, new YourOperation1(), ...)` You can add a method, Method signature like this: `public void name(RedisRequest request) throws IOException` Method name is same as your command, and Method names are all lowercase. In `RedisRequest` , you can get RedisServer -> Scheme -> database and so on ! You can create an issue also, I will add it as much as I can. Good luck !