Unity访问Redis

1.条件

下载 TeamDev.Redis.dll , 下载完毕后放到项目Plugins目录内。

2.基本代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TeamDev.Redis;

public class RedisDemo : MonoBehaviour {
	private RedisDataAccessProvider redis;
	private string[] keys;

	// Use this for initialization
	void Start () {
		redis = new RedisDataAccessProvider ();
		redis.Configuration.Host = "127.0.0.1";
		redis.Configuration.Port = 6379;
		redis.Connect ();

		redis.SendCommand (RedisCommand.KEYS, "*");
		keys = redis.ReadMultiString ();

		//打印所有的key
		foreach (var item in keys) {
			Debug.Log (item);
		}

		//频道订阅
		redis.ChannelSubscribed +=  new ChannelSubscribedHandler(redisDataAccessProvider_ChannelSubscribed);
		redis.MessageReceived +=  new MessageReceivedHandler(redisDataAccessProvider_MessageReceived);
		redis.Messaging.Subscribe("channeltest");
	}

	static void redisDataAccessProvider_MessageReceived(string channelname, string message){
		Debug.Log (message);
	}

	static void redisDataAccessProvider_ChannelSubscribed(string channelname){
		Debug.Log (channelname);
	}
}

 

你可能感兴趣的:(Unity)