You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange;
using StackExchange.Redis;
namespace RedisStuy
{
/// <summary>
/// Redis Database 操作
/// https://stackexchange.github.io/StackExchange.Redis/
/// </summary>
public class RedisDatabaseStudy : IDisposable
{
private IDatabase redisDatabase;
public RedisDatabaseStudy()
{
redisDatabase = RedisHelper.GetRedisDatabase();
}
public void Dispose()
{
}
#region key 操作
/// <summary>
/// 删除指定的key, 不存在则忽略
/// </summary>
public bool KeyDelete(RedisKey key)
{
return redisDatabase.KeyDelete(key);
}
/// <summary>
/// 删除指定的key, 不存在则忽略
/// </summary>
public async Task<bool> KeyDeleteAsync(RedisKey key)
{
return await redisDatabase.KeyDeleteAsync(key);
}
/// <summary>
/// 序列化指定的key并返回被序列化的值。
/// </summary>
public byte[] KeyDump(RedisKey key)
{
return redisDatabase.KeyDump(key);
}
/// <summary>
/// 指定的key,是否存在
/// </summary>
public bool KeyExists(RedisKey key)
{
return redisDatabase.KeyExists(key);
}
/// <summary>
/// 为给定 key 设置生存时间
/// </summary>
public bool KeyExpire(RedisKey key, TimeSpan? expire)
{
return redisDatabase.KeyExpire(key, expire);
}
/// <summary>
/// 重命名key
/// </summary>
public bool KeyRename(RedisKey sourceKey, RedisKey newKey)
{
return redisDatabase.KeyRename(key: sourceKey, newKey: newKey);
}
/// <summary>
/// 迁移key到另外数据库
/// </summary>
/// <param name="key">key</param>
/// <param name="toServer">目标服务器</param>
/// <param name="toDatabase">目标数据库</param>
/// <param name="timeoutMilliseconds">操作超时豪秒数</param>
/// <param name="migrateOptions">迁移选项</param>
/// <param name="flags">命令标记</param>
public void KeyMigrate(RedisKey key, System.Net.EndPoint toServer, int toDatabase = 0, int timeoutMilliseconds = 0, MigrateOptions migrateOptions = MigrateOptions.None, CommandFlags flags = CommandFlags.None)
{
redisDatabase.KeyMigrate(key, toServer, toDatabase, timeoutMilliseconds, migrateOptions, flags);
}
/// <summary>
/// 移动key到同服务器的另一个数据库
/// </summary>
public bool KeyMove(RedisKey key, int databaseIndex)
{
return redisDatabase.KeyMove(key, databaseIndex);
}
/// <summary>
/// 删除键所指定的过期时间
/// </summary>
public bool KeyPersist(RedisKey key)
{
return redisDatabase.KeyPersist(key);
}
/// <summary>
/// 从当前数据库中随机返回一个key
/// </summary>
public RedisKey KeyRandom()
{
return redisDatabase.KeyRandom();
}
/// <summary>
/// 反序列化给定的序列化值,并将它和给定的 key 关联
/// </summary>
/// <param name="expiry">以毫秒为单位为 key 设置生存时间;为null,不设置生存时间</param>
/// <returns>
/// 是否执行成功
/// </returns>
public bool KeyRestore(RedisKey key, byte[] value,TimeSpan? expiry,CommandFlags flags)
{
bool isSucess = false;
try
{
redisDatabase.KeyRestore(key, value, expiry, flags);
isSucess = true;
}
catch
{
isSucess = false;
}
return isSucess;
}
/// <summary>
/// 获取具有超时的键的剩余生存时间
/// </summary>
public TimeSpan? KeyTimeToLive(RedisKey key)
{
return redisDatabase.KeyTimeToLive(key);
}
/// <summary>
/// 获取指定key的数据类型
/// </summary>
public RedisType KeyType(RedisKey key)
{
var redisType = redisDatabase.KeyType(key);
return redisType;
}
#endregion
}
}