Commit e26d0cac authored by liudong1993's avatar liudong1993

redis 位图操作

parent 17445e3d
......@@ -278,6 +278,114 @@ namespace Mall.CacheManager.Base
#endregion String
#region bitmap
/// <summary>
/// 设置
/// </summary>
/// <param name="uid"></param>
/// <param name="date"></param>
/// <returns></returns>
public bool SetSignBit(int uid, DateTime date)
{
var database = _conn.GetDatabase(DbNum);
int offset = date.Day - 1;
return database.StringSetBit(buildSignKey(uid, date), offset, true);
}
/// <summary>
/// 获取是否已经存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool GetSignBit(int uid, DateTime date)
{
var database = _conn.GetDatabase(DbNum);
int offset = date.Day - 1;
return database.StringGetBit(buildSignKey(uid, date), offset);
}
public string GetBitStr(int uid, DateTime date)
{
var database = _conn.GetDatabase(DbNum);
return database.StringGet(buildSignKey(uid, date));
}
public bool GetSignBit(int uid, DateTime date, IDatabase database)
{
int offset = date.Day - 1;
return database.StringGetBit(buildSignKey(uid, date), offset);
}
/// <summary>
/// 获取签到数量
/// </summary>
/// <param name="uid"></param>
/// <param name="date"></param>
/// <returns></returns>
public long GetSignCount(int uid, DateTime date)
{
var database = _conn.GetDatabase(DbNum);
return database.StringBitCount(buildSignKey(uid, date));
}
/// <summary>
/// 获取当月第一次签到日期
/// </summary>
/// <param name="uid"></param>
/// <param name="date"></param>
/// <returns></returns>
public string getFirstSignDate(int uid, DateTime date)
{
var database = _conn.GetDatabase(DbNum);
long pos = database.StringBitPosition(buildSignKey(uid, date), true);
return pos < 0 ? "" : date.ToString("yyyy-MM") + "-" + (pos + 1);
}
/// <summary>
/// 当月连续签到
/// </summary>
/// <param name="uid"></param>
/// <param name="Month"></param>
/// <returns></returns>
public long getContinuousSignCount(int uid, DateTime date)
{
int signCount = 0;
var database = _conn.GetDatabase(DbNum);
DateTime StartTime = Convert.ToDateTime(date.ToString("yyyy-MM") + "-01");
DateTime EndTime = Convert.ToDateTime(date.ToString("yyyy-MM-dd"));
//遍历
while (StartTime <= EndTime)
{
if (GetSignBit(uid, EndTime, database))
{
signCount++;
}
else
{
if (EndTime.ToString("yyyy-MM-dd") != date.ToString("yyyy-MM-dd"))
{
break;
}
}
EndTime = EndTime.AddDays(-1);
}
return signCount;
}
private static String buildSignKey(int uid, DateTime date)
{
return String.Format("u:sign:{0}:{1}", uid, formatDate(date));
}
private static String formatDate(DateTime date)
{
return date.ToString("yyyyMM");
}
#endregion
#region Hash
#region 同步方法
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment