tijian_tieying/web/cyqdata-master/Cache/Redis/RedisCommand.cs

105 lines
2.5 KiB
C#
Raw Normal View History

2025-02-20 12:14:39 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CYQ.Data.Cache
{
/*
redis允许客户端以TCP方式连接6379\r\n结尾
*<number of arguments>\r\n$<number of bytes of argument 1>\r\n<argument data>\r\n
*1\r\n$4\r\nINFO\r\n
1 ++OK\r\n
2:           - -ERR unknown command 'mush'\r\n
3: : :1\r\n
4512M $+ $4\r\mush\r\n
5 * *2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n
*/
/// <summary>
/// RedisCommand
/// </summary>
internal class RedisCommand : IDisposable
{
MSocket socket;
public RedisCommand(MSocket socket, int commandCount, string command)
{
this.socket = socket;
Write(string.Format("*{0}\r\n", commandCount));
WriteKey(command);
}
private void Write(string cmd)
{
WriteData(Encoding.UTF8.GetBytes(cmd));
}
private void WriteHeader(int bodyLen)
{
Write("$" + bodyLen + "\r\n");
}
public void WriteKey(string cmd)
{
byte[] data = Encoding.UTF8.GetBytes(cmd);
WriteHeader(data.Length);
WriteData(data);
Write("\r\n");
}
public void WriteValue(string value)
{
WriteKey(value);
}
public void WriteValue(byte[] data1)
{
WriteValue(data1, null);
}
public void WriteValue(byte[] data1, byte[] data2)
{
int len = data1.Length;
if (data2 != null)
{
len += data2.Length;
}
WriteHeader(len);
WriteData(data1);
if (data2 != null)
{
WriteData(data2);
}
Write("\r\n");
}
private void WriteData(byte[] cmd)
{
socket.Write(cmd);
}
public void Reset(int commandCount, string command)
{
Write(string.Format("*{0}\r\n", commandCount));
WriteKey(command);
}
public void Dispose()
{
}
}
}