VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之StackExchange.Redis 二次封装(2)

/// <param name="key"></param> 237 /// <returns></returns> 238 public static T HashGet<T>(int dbIndex, string hashId, string key) 239 { 240 var db = Instance.GetDatabase(dbIndex); 241 string value = db.HashGet(hashId, key); 242 if (string.IsNullOrWhiteSpace(value)) 243 return default(T); 244 return JsonConvert.DeserializeObject<T>(value); 245 } 246 247 /// <summary> 248 /// 获取哈希值 249 /// </summary> 250 /// <param name="dbIndex">数据库</param> 251 /// <param name="hashId">哈希ID</param> 252 /// <param name="key"></param> 253 /// <returns></returns> 254 public static string HashGet(int dbIndex, string hashId, string key) 255 { 256 var db = Instance.GetDatabase(dbIndex); 257 string value = db.HashGet(hashId, key).ToString(); 258 return value; 259 } 260 261 /// <summary> 262 /// 获取哈希值的所有键 263 /// </summary> 264 /// <param name="dbIndex">数据库</param> 265 /// <param name="hashId">哈希ID</param> 266 /// <returns></returns> 267 public static List<string> HashKeys(int dbIndex, string hashId) 268 { 269 var db = Instance.GetDatabase(dbIndex); 270 var result = new List<string>(); 271 var list = db.HashKeys(hashId).ToList(); 272 if (list.Count > 0) 273 { 274 list.ForEach(x => 275 { 276 var value = JsonConvert.DeserializeObject<string>(x); 277 result.Add(value); 278 }); 279 } 280 return result; 281 } 282 283 /// <summary> 284 /// 获取所有哈希值 285 /// </summary> 286 /// <typeparam name="T">哈希值类型</typeparam> 287 /// <param name="dbIndex">数据库</param> 288 /// <param name="hashId">哈希ID</param> 289 /// <returns></returns> 290 public static List<T> HashGetAll<T>(int dbIndex, string hashId) 291 { 292 var db = Instance.GetDatabase(dbIndex); 293 var result = new List<T>(); 294 var list = db.HashGetAll(hashId).ToList(); 295 if (list.Count > 0) 296 { 297 list.ForEach(x => 298 { 299 var value = JsonConvert.DeserializeObject<T>(x.Value); 300 result.Add(value); 301 }); 302 } 303 return result; 304 } 305 306 /// <summary> 307 /// 删除哈希值 308 /// </summary> 309 /// <param name="dbIndex">数据库</param> 310 /// <param name="hashId">哈希ID</param> 311 /// <param name="key"></param> 312 /// <returns></returns> 313 public static bool HashDelete(int dbIndex, string hashId, string key) 314 { 315 var db = Instance.GetDatabase(dbIndex); 316 return db.HashDelete(hashId, key); 317 } 318 #endregion 319 320 #region Lists 321 /// <summary> 322 /// 集合长度 323 /// </summary> 324 /// <param name="dbIndex">数据库</param> 325 /// <param name="listId">集合ID</param> 326 /// <returns></returns> 327 public static long ListLength(int dbIndex, string listId) 328 { 329 var db = Instance.GetDatabase(dbIndex); 330 return db.ListLength(listId); 331 } 332 333 /// <summary> 334 /// 向集合中添加元素 335 /// </summary> 336 /// <typeparam name="T">元素类型</typeparam> 337 /// <param name="dbIndex">数据库</param> 338 /// <param name="listId">集合ID</param> 339 /// <param name="list">元素值</param> 340 /// <returns></returns> 341 public static long AddList<T>(int dbIndex, string listId, List<T> list) 342 { 343 var db = Instance.GetDatabase(dbIndex); 344 if (list != null && list.Count > 0) 345 { 346 foreach (var item in list) 347 { 348 db.ListRightPush(listId, JsonConvert.SerializeObject(item)); 349 } 350 } 351 return ListLength(dbIndex, listId); 352 } 353 354 /// <summary> 355 /// 获取集合元素(默认获取整个集合) 356 /// </summary> 357 /// <typeparam name="T">元素类型</typeparam> 358 /// <param name="dbIndex">数据库</param> 359 /// <param name="listId">集合ID</param> 360 /// <param name="start">起始位置(0表示第1个位置)</param> 361 /// <param name="stop">结束位置(-1表示倒数第1个位置)</param> 362 /// <returns></returns> 363 public static List<T> GetList<T>(int dbIndex, string listId, long start = 0, long stop = -1) 364 { 365 var db = Instance.GetDatabase(dbIndex); 366 var result = new List<T>(); 367 var list = db.ListRange(listId, start, stop).ToList(); 368 if (list.Count > 0) 369 { 370 list.ForEach(x => 371 { 372 var value = JsonConvert.DeserializeObject<T>(x); 373 result.Add(value); 374 }); 375 } 376 return result; 377 } 378 #endregion 379 380 #region ZSet 381 382 #region 同步方法 383 384 /// <summary> 385 /// 添加一个值到Key 386 /// </summary> 387 /// <typeparam name="T"></typeparam> 388 /// <param name="dbIndex">数据库</param> 389 /// <param name="key"></param> 390 /// <param name="value"></param> 391 /// <param name="score">排序分数,为空将获取集合中最大score加1</param> 392 /// <returns></returns> 393 public static bool SortedSetAdd<T>(int dbIndex, string key, T value, double? score = null) 394 { 395 var db = Instance.GetDatabase(dbIndex); 396 double scoreNum = score ?? _GetScore(key, db); 397 return db.SortedSetAdd(key, ConvertJson<T>(value), scoreNum); 398 } 399 400 /// <summary> 401 /// 添加一个集合到Key 402 /// </summary> 403 /// <typeparam name="T"></typeparam> 404 /// <param name="dbIndex">数据库</param> 405 /// <param name="key"></param> 406 /// <param name="value"></param> 407 /// <param name="score">排序分数,为空将获取集合中最大score加1</param> 408 /// <returns></returns> 409 public static long SortedSetAdd<T>(int dbIndex, string key, List<T> value, double? score = null) 410 { 411 var db = Instance.GetDatabase(dbIndex); 412 double scoreNum = score ?? _GetScore(key, db); 413 SortedSetEntry[] rValue = value.Select(o => new SortedSetEntry(ConvertJson<T>(o), scoreNum++)).ToArray(); 414 return db.SortedSetAdd(key, rValue); 415 } 416 417 /// <summary> 418 /// 获取集合中的数量 419 /// </summary> 420 /// <param name="dbIndex">数据库</param> 421 /// <param name="key"></param> 422 /// <returns></returns> 423 public static long SortedSetLength(int dbIndex,string key) 424 { 425 var db = Instance.GetDatabase(dbIndex); 426 return db.SortedSetLength(key); 427 } 428 429 /// <summary> 430 /// 获取指定起始值到结束值的集合数量 431 /// </summary> 432 /// <typeparam name="T"></typeparam> 433 /// <param name="dbIndex">数据库</param> 434 /// <param name="key"></param> 435 /// <param name="startValue">起始值</param> 436 /// <param name="endValue">结束值</param> 437 /// <returns></returns> 438 public static long SortedSetLengthByValue<T>(int dbIndex, string key, T startValue, T endValue) 439 { 440 var db = Instance.GetDatabase(dbIndex); 441 var sValue = ConvertJson<T>(startValue); 442 var eValue = ConvertJson<T>(endValue); 443 return db.SortedSetLengthByValue(key, sValue, eValue); 444 } 445 446 /// <summary> 447 /// 获取指定Key的排序Score值 448 /// </summary> 449 /// <typeparam name="T"></typeparam> 450 /// <param name="dbIndex">数据库</param> 451 /// <param name="key"></param> 452 /// <param name="value"></param> 453 /// <returns></returns> 454 public static double? SortedSetScore<T>(int dbIndex, string key, T value) 455 { 456 var db = Instance.GetDatabase(dbIndex); 457 var rValue = ConvertJson<T>(value); 458 return db.SortedSetScore(key, rValue); 459 } 460 461 /// <summary> 462 /// 获取指定Key中最小Score值 463 /// </summary> 464 /// <param name="dbIndex">数据库</param> 465 /// <param name="key"></param> 466 /// <returns></returns> 467 public static double SortedSetMinScore(int dbIndex, string key) 468 { 469 var db = Instance.GetDatabase(dbIndex); 470 double dValue = 0; 471 var rValue = db.SortedSetRangeByRankWithScores(key, 0, 0, Order.Ascending).FirstOrDefault(); 472 dValue = rValue != null ? rValue.Score : 0; 473 return dValue; 474 }

相关教程