Skip to content

Commit

Permalink
- 增加 Get 缓冲读取大对象重载方法;#304
Browse files Browse the repository at this point in the history
  • Loading branch information
28810 authored and 28810 committed Jun 9, 2020
1 parent 0028d9a commit bfd3d58
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/CSRedisCore/CSRedisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.IO;

namespace CSRedis
{
Expand Down Expand Up @@ -3288,6 +3289,21 @@ public long BitOp(RedisBitOp op, string destKey, params string[] keys)
/// <returns></returns>
public T Get<T>(string key) => this.DeserializeRedisValueInternal<T>(ExecuteScalar(key, (c, k) => c.Value.GetBytes(k)));
/// <summary>
/// 获取指定 key 的值(适用大对象返回)
/// </summary>
/// <param name="key">不含prefix前辍</param>
/// <param name="destination">读取后写入目标流中</param>
/// <param name="bufferSize">读取缓冲区</param>
public void Get(string key, Stream destination, int bufferSize = 1024)
{
ExecuteScalar(key, (c, k) =>
{
c.Value.WriteNoneRead(new Internal.Commands.RedisString("GET", k));
c.Value._reader.ReadBulkBytes(destination, bufferSize, true);
return true;
});
}
/// <summary>
/// 对 key 所储存的值,获取指定偏移量上的位(bit)
/// </summary>
/// <param name="key">不含prefix前辍</param>
Expand Down
2 changes: 1 addition & 1 deletion src/CSRedisCore/CSRedisCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>CSRedisCore</AssemblyName>
<PackageId>CSRedisCore</PackageId>
<RootNamespace>CSRedisCore</RootNamespace>
<Version>3.6.3</Version>
<Version>3.6.5</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/csredis</PackageProjectUrl>
<Description>CSRedis 是 redis.io 官方推荐库,支持 redis-trib集群、哨兵、私有分区与连接池管理技术,简易 RedisHelper 静态类。</Description>
Expand Down
24 changes: 23 additions & 1 deletion src/CSRedisCore/Internal/RedisConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RedisConnector
readonly int _bufferSize;
internal readonly IRedisSocket _redisSocket;
readonly EndPoint _endPoint;
readonly RedisIO _io;
internal readonly RedisIO _io;

public event EventHandler Connected;

Expand Down Expand Up @@ -105,6 +105,28 @@ public T Call<T>(RedisCommand<T> command)
}
}

public void CallNoneRead(RedisCommand command)
{
ConnectIfNotConnected();

try
{
//Console.WriteLine("--------------Call " + command.ToString());
_io.Write(_io.Writer.Prepare(command));
}
catch (IOException)
{
if (ReconnectAttempts == 0)
throw;
Reconnect();
CallNoneRead(command);
}
catch (RedisException ex)
{
throw new RedisException($"{ex.Message}\r\nCommand: {command}", ex);
}
}

#if net40
#else
async public Task<T> CallAsync<T>(RedisCommand<T> command)
Expand Down
2 changes: 2 additions & 0 deletions src/CSRedisCore/RedisClient.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ T Write<T>(RedisCommand<T> command)
return _connector.Call(command);
}

internal void WriteNoneRead(RedisCommand command) => _connector.CallNoneRead(command);

#region Connection
/// <summary>
/// Authenticate to the server
Expand Down
1 change: 1 addition & 0 deletions src/CSRedisCore/RedisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public partial class RedisClient : IRedisClientSync, IRedisClientAsync
readonly SubscriptionListener _subscription;
readonly MonitorListener _monitor;
bool _streaming;
internal RedisReader _reader => _connector?._io?.Reader;

internal Socket Socket => (_connector?._redisSocket as RedisSocket)?._socket;

Expand Down
8 changes: 8 additions & 0 deletions src/CSRedisCore/RedisHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.IO;

public abstract class RedisHelper : RedisHelper<RedisHelper> { }

Expand Down Expand Up @@ -1355,6 +1356,13 @@ public static RedisScan<T> SScan<T>(string key, long cursor, string pattern = nu
/// <returns></returns>
public static T Get<T>(string key) => Instance.Get<T>(key);
/// <summary>
/// 获取指定 key 的值(适用大对象返回)
/// </summary>
/// <param name="key">不含prefix前辍</param>
/// <param name="destination">读取后写入目标流中</param>
/// <param name="bufferSize">读取缓冲区</param>
public static void Get(string key, Stream destination, int bufferSize = 1024) => Instance.Get(key, destination, bufferSize);
/// <summary>
/// 对 key 所储存的值,获取指定偏移量上的位(bit)
/// </summary>
/// <param name="key">不含prefix前辍</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>Caching.CSRedis</AssemblyName>
<PackageId>Caching.CSRedis</PackageId>
<RootNamespace>Caching.CSRedis</RootNamespace>
<Version>3.6.3</Version>
<Version>3.6.5</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/csredis/tree/master/src/Microsoft.Extensions.Caching.CSRedis/README.md</PackageProjectUrl>
<Description>分布式缓存 CSRedisCore 实现 Microsoft.Extensions.Caching</Description>
Expand Down
5 changes: 5 additions & 0 deletions test/CSRedisCore.Tests/CSRedisClientStringTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -19,6 +20,10 @@ public void Append() {
rds.Set(key, base.String);
rds.Append(key, base.String);
Assert.Equal(rds.Get(key), base.String + base.String);
var ms = new MemoryStream();
rds.Get(key, ms);
Assert.Equal(Encoding.UTF8.GetString(ms.ToArray()), base.String + base.String);
ms.Close();

key = "TestAppend_bytes";
rds.Set(key, base.Bytes);
Expand Down

0 comments on commit bfd3d58

Please sign in to comment.