Redis 中文文档 Redis 中文文档
指南
redis.io (opens new window)
指南
redis.io (opens new window)
  • 关于
    • Redis 开源治理
    • Redis 发布周期
    • Redis 赞助商
  • 入门
  • 数据类型
  • Redis Stack
  • 命令
  • 手册

RedisDB on Android


Redis version


May/23/2019  Redis 5.0.5

Description


Redis is known for NoSQL database. You can run Redis database on your server.

This library helps you to run Redis database on Android device.

This library is just cross-compiled the original redis database for Android architectures such as ARM cpu.

You can run Redis database in background thread in your application.

Benefit of running Redis DB on Android


There are some benefits for running Redis DB on Android.

NoSQL database in memory

The most remarkable feature of the Redis DB is NoSQL. It means you can easily store data with key, and retrieve it (just like JSON object).

No Internet is required

MongoDB or Google Firebase are known for NoSQL hosted on their cloud servers. However they need to connect to their server through the Internet. It is depends on your purpose, but I wanted own hosted database on Android.

TTL for key

One of my favorite points of Redis DB is you are able to set TTL (time to live). You can save the key-value pairs that is available only in TTL time.

Publish/Subscriber

One of the reason I created this library is Redis DB has publisher/subscriber mechanism, like a chat room. You can notify your message from one application, and receive the data on another applications, even on multiple devices.

No dependencies

One of the reason I gave up to use Google Firebase is that the library requires Google Play Services. But my Android does not have it (AOSP rom). You can run Redis DB with only this library.

Install


In order to use this library, you need to add the below two lines into your build.gradle file.

build.gradle

  1. ``` sh
  2. repositories {
  3.     maven { url "https://raw.githubusercontent.com/wf9a5m75/redis-android/master/repository/" }
  4. }

  5. dependencies {
  6.     compile 'io.wf9a5m75:redis-android:1.1.6'
  7. }

  8. ```

How to use this in your app?


  1. ``` java
  2. import io.wf9a5m75.redis.RedisAndroid;

  3. public class MyService extends IntentService {

  4.   public MyService() {
  5.     super("MyService");
  6.   }

  7.   @Nullable
  8.   @Override
  9.   public IBinder onBind(Intent intent) {
  10.     return null;
  11.   }

  12.   @Override
  13.   protected void onHandleIntent(@Nullable Intent intent) {

  14.     Bundle configs = new Bundle();
  15.     configs.putString("port", "6379");   // <-- strongly recommend to change to different port number
  16.     configs.putString("protected-mode", "no");
  17.     configs.putString("requirepass", "");
  18.     RedisAndroid.start(this, configs);
  19.   }
  20. }
  21. ```

Redis settings


This library specifies default settings for Android in the RedisAndroid.java.

You can overwrite these settings for your purpose.

For example, the default maxmemory = 10mb, but if you want to increase it,

  1. ``` java
  2. Bundle configs = new Bundle();
  3. configs.putString("port", "6379");   // <-- strongly recommend to change to different port number
  4. configs.putString("maxmemory", "30mb");
  5. configs.putString("protected-mode", "no");
  6. configs.putString("requirepass", "");
  7. RedisAndroid.start(this, configs);
  8. ```

The details of settings are defined at the http://download.redis.io/redis-stable/redis.conf

How to connect to the Redis DB from your app?


You need to use redis client libraries.

https://redis.io/clients#java

redis-cli command


You need to install the command by your hand. You may need ROOTpermission of your device.

(redis-check-aof, redis-check-rdb commands are also the same steps)

Download the pre-build-libs.zip file from here.

https://github.com/wf9a5m75/redis-android/releases

Extract it

push the redis-cli file into your device.

  1. ``` sh
  2. $> adb root

  3. $> adb remount // or adb shell 'mount -o rw,remount /system'

  4. $> adb push redis-cli /system/xbin/redis-cli

  5. $> adb shell chmod 0755 /system/xbin/redis-cli

  6. $> adb shell chown root.shell /system/xbin/redis-cli

  7. $> adb shell

  8. #> redis-cli

  9. ```

Playing with Redis


  1. ``` sh
  2. redis-cli -h 192.168.86.23
  3. 192.168.86.23:6379> ping
  4. PONG
  5. 192.168.86.23:6379> set foo bar
  6. OK
  7. 192.168.86.23:6379> get foo
  8. "bar"
  9. 192.168.86.23:6379> incr mycounter
  10. (integer) 1
  11. 192.168.86.23:6379> incr mycounter
  12. (integer) 2
  13. 192.168.86.23:6379> keys *
  14. 1) "mycounter"
  15. 2) "foo"
  16. 192.168.86.23:6379> get mycounter
  17. "2"
  18. 192.168.86.23:6379>

  19. ```


Version compatibles


Redis version redis-android version
:--- :---
Redis 5.0.5 v1.1.6
Redis 5.0.4 v1.1.5
Redis 5.0.3 v1.1.4
Redis 5.0.2 v1.1.3
Redis 5.0.1 v1.1.1 - v1.1.2
Redis 5.0.0 v1.1.0
Redis 4.0.11 v1.0.8
Redis 4.0.10 v1.0.7
Redis 4.0.9 v1.0.6
Redis 4.0.8 v1.0.4 - v1.0.5
Redis 4.0.6 v1.0.0 - v1.0.3
Last Updated: 2023-09-03 19:17:54