github.com/songzhibin97/gkit@v1.2.13/cache/local_cache/cache.go (about) 1 package local_cache 2 3 import ( 4 "context" 5 "encoding/gob" 6 "fmt" 7 "io" 8 "os" 9 "sync" 10 "time" 11 12 "github.com/songzhibin97/gkit/options" 13 ) 14 15 const ( 16 DefaultExpire time.Duration = 0 17 NoExpire time.Duration = -1 18 ) 19 20 type Cache struct { 21 *cache 22 } 23 24 func NewCache(options ...options.Option) Cache { 25 ctx, cancel := context.WithCancel(context.Background()) 26 c := &Config{ 27 defaultExpire: 0, 28 interval: 0, 29 capture: func(k string, v interface{}) { 30 fmt.Printf("delete k:%s v:%v\n", k, v) 31 }, 32 } 33 for _, option := range options { 34 option(c) 35 } 36 obj := &cache{ 37 defaultExpire: c.defaultExpire, 38 capture: c.capture, 39 cancel: cancel, 40 } 41 if c.member == nil { 42 c.member = map[string]Iterator{} 43 } 44 if c.fn == nil { 45 c.fn = obj.DeleteExpire 46 } 47 obj.member = c.member 48 obj.sentinel = NewSentinel(ctx, c.interval, c.fn) 49 go obj.sentinel.Start() 50 return Cache{obj} 51 } 52 53 type cache struct { 54 sync.RWMutex 55 56 // sentinel 维护一个周期循环的任务 57 *sentinel 58 59 // defaultExpire 默认超时时间 60 defaultExpire time.Duration 61 62 // member 维护map存储kv关系 63 member map[string]Iterator 64 65 // capture 捕获删除对象时间 会返回kv值用于用户自定义处理 66 capture func(k string, v interface{}) 67 68 cancel context.CancelFunc 69 } 70 71 // Set 添加cache 无论是否存在都会覆盖 72 func (c *cache) Set(k string, v interface{}, d time.Duration) { 73 var expire int64 74 switch d { 75 case NoExpire: 76 case DefaultExpire: 77 if c.defaultExpire > 0 { 78 expire = time.Now().Add(c.defaultExpire).UnixNano() 79 } 80 default: 81 if d > 0 { 82 expire = time.Now().Add(d).UnixNano() 83 } 84 // 如果走到这里 默认是 NoExpire 85 } 86 c.Lock() 87 c.member[k] = Iterator{ 88 Val: v, 89 Expire: expire, 90 } 91 c.Unlock() 92 } 93 94 // set 添加cache 无论是否存在都会覆盖 内部无锁 95 func (c *cache) set(k string, v interface{}, d time.Duration) { 96 var expire int64 97 switch d { 98 case NoExpire: 99 case DefaultExpire: 100 if c.defaultExpire > 0 { 101 expire = time.Now().Add(c.defaultExpire).UnixNano() 102 } 103 default: 104 if d > 0 { 105 expire = time.Now().Add(d).UnixNano() 106 } 107 // 如果走到这里 默认是 NoExpire 108 } 109 c.member[k] = Iterator{ 110 Val: v, 111 Expire: expire, 112 } 113 } 114 115 // SetDefault 添加cache 无论是否存在都会覆盖 超时设置为创建cache的默认时间 116 func (c *cache) SetDefault(k string, v interface{}) { 117 c.Set(k, v, DefaultExpire) 118 } 119 120 // SetNoExpire 添加cache 无论是否存在都会覆盖 超时设置为0 121 func (c *cache) SetNoExpire(k string, v interface{}) { 122 c.Set(k, v, NoExpire) 123 } 124 125 // Get 根据key获取 cache 126 func (c *cache) Get(k string) (interface{}, bool) { 127 c.RLock() 128 if v, ok := c.member[k]; !ok { 129 c.RUnlock() 130 return nil, false 131 } else { 132 if v.Expired() { 133 c.RUnlock() 134 c.Delete(k) 135 return nil, false 136 } else { 137 c.RUnlock() 138 return v.Val, true 139 } 140 } 141 } 142 143 // get 根据key获取 cache 144 func (c *cache) get(k string) (interface{}, bool) { 145 if v, ok := c.member[k]; !ok { 146 return nil, false 147 } else { 148 if v.Expired() { 149 c._delete(k) 150 return nil, false 151 } 152 c._delete(k) 153 return v.Val, true 154 } 155 } 156 157 // GetWithExpire 根据key获取 cache 并带出超时时间 158 func (c *cache) GetWithExpire(k string) (interface{}, time.Time, bool) { 159 c.RLock() 160 if v, ok := c.member[k]; !ok { 161 c.RUnlock() 162 return nil, time.Time{}, false 163 } else { 164 if v.Expired() { 165 c.RUnlock() 166 c.Delete(k) 167 return nil, time.Time{}, false 168 } 169 c.RUnlock() 170 if v.Expire > 0 { 171 return v.Val, time.Unix(0, v.Expire), true 172 } 173 return v.Val, time.Time{}, true 174 } 175 } 176 177 // Add 添加cache 如果存在则抛出异常 178 func (c *cache) Add(k string, x interface{}, d time.Duration) error { 179 c.Lock() 180 if _, ok := c.get(k); ok { 181 c.Unlock() 182 return CacheExist 183 } 184 c.set(k, x, d) 185 c.Unlock() 186 return nil 187 } 188 189 // Replace 替换cache 如果有就设置没有就抛出异常 190 func (c *cache) Replace(k string, x interface{}, d time.Duration) error { 191 c.Lock() 192 if _, ok := c.get(k); !ok { 193 c.Unlock() 194 return CacheNoExist 195 } 196 c.set(k, x, d) 197 c.Unlock() 198 return nil 199 } 200 201 // Increment 为k对应的value增加n n必须为数字类型 202 func (c *cache) Increment(k string, n int64) error { 203 c.Lock() 204 if v, ok := c.member[k]; !ok { 205 c.Unlock() 206 return CacheNoExist 207 } else { 208 if v.Expired() { 209 c.Unlock() 210 c.Delete(k) 211 return CacheExpire 212 } 213 switch v.Val.(type) { 214 case int: 215 v.Val = v.Val.(int) + int(n) 216 case int8: 217 v.Val = v.Val.(int8) + int8(n) 218 case int16: 219 v.Val = v.Val.(int16) + int16(n) 220 case int32: 221 v.Val = v.Val.(int32) + int32(n) 222 case int64: 223 v.Val = v.Val.(int64) + n 224 case uint: 225 v.Val = v.Val.(uint) + uint(n) 226 case uintptr: 227 v.Val = v.Val.(uintptr) + uintptr(n) 228 case uint8: 229 v.Val = v.Val.(uint8) + uint8(n) 230 case uint16: 231 v.Val = v.Val.(uint16) + uint16(n) 232 case uint32: 233 v.Val = v.Val.(uint32) + uint32(n) 234 case uint64: 235 v.Val = v.Val.(uint64) + uint64(n) 236 case float32: 237 v.Val = v.Val.(float32) + float32(n) 238 case float64: 239 v.Val = v.Val.(float64) + float64(n) 240 default: 241 c.Unlock() 242 return CacheTypeErr 243 } 244 c.member[k] = v 245 c.Unlock() 246 return nil 247 } 248 } 249 250 // IncrementFloat 为k对应的value增加n n必须为浮点数类型 251 func (c *cache) IncrementFloat(k string, n float64) error { 252 c.Lock() 253 if v, ok := c.member[k]; !ok { 254 c.Unlock() 255 return CacheNoExist 256 } else { 257 if v.Expired() { 258 c.Unlock() 259 c.Delete(k) 260 return CacheExpire 261 } 262 switch v.Val.(type) { 263 case float32: 264 v.Val = v.Val.(float32) + float32(n) 265 case float64: 266 v.Val = v.Val.(float64) + n 267 default: 268 c.Unlock() 269 return CacheTypeErr 270 } 271 c.member[k] = v 272 c.Unlock() 273 return nil 274 } 275 } 276 277 // IncrementInt 为k对应的value增加n n必须为int类型 278 func (c *cache) IncrementInt(k string, n int) (int, error) { 279 c.Lock() 280 if v, ok := c.member[k]; !ok { 281 c.Unlock() 282 return 0, CacheNoExist 283 } else { 284 if v.Expired() { 285 c.Unlock() 286 c.Delete(k) 287 return 0, CacheExpire 288 } 289 if i, ok := v.Val.(int); !ok { 290 c.Unlock() 291 return 0, CacheTypeErr 292 } else { 293 ret := i + n 294 v.Val = ret 295 c.member[k] = v 296 c.Unlock() 297 return ret, nil 298 } 299 } 300 } 301 302 // IncrementInt8 为k对应的value增加n n必须为int8类型 303 func (c *cache) IncrementInt8(k string, n int8) (int8, error) { 304 c.Lock() 305 if v, ok := c.member[k]; !ok { 306 c.Unlock() 307 return 0, CacheNoExist 308 } else { 309 if v.Expired() { 310 c.Unlock() 311 c.Delete(k) 312 return 0, CacheExpire 313 } 314 if i, ok := v.Val.(int8); !ok { 315 c.Unlock() 316 return 0, CacheTypeErr 317 } else { 318 ret := i + n 319 v.Val = ret 320 c.member[k] = v 321 c.Unlock() 322 return ret, nil 323 } 324 } 325 } 326 327 // IncrementInt16 为k对应的value增加n n必须为int16类型 328 func (c *cache) IncrementInt16(k string, n int16) (int16, error) { 329 c.Lock() 330 if v, ok := c.member[k]; !ok { 331 c.Unlock() 332 return 0, CacheNoExist 333 } else { 334 if v.Expired() { 335 c.Unlock() 336 c.Delete(k) 337 return 0, CacheExpire 338 } 339 if i, ok := v.Val.(int16); !ok { 340 c.Unlock() 341 return 0, CacheTypeErr 342 } else { 343 ret := i + n 344 v.Val = ret 345 c.member[k] = v 346 c.Unlock() 347 return ret, nil 348 } 349 } 350 } 351 352 // IncrementInt32 为k对应的value增加n n必须为int32类型 353 func (c *cache) IncrementInt32(k string, n int32) (int32, error) { 354 c.Lock() 355 if v, ok := c.member[k]; !ok { 356 c.Unlock() 357 return 0, CacheNoExist 358 } else { 359 if v.Expired() { 360 c.Unlock() 361 c.Delete(k) 362 return 0, CacheExpire 363 } 364 if i, ok := v.Val.(int32); !ok { 365 c.Unlock() 366 return 0, CacheTypeErr 367 } else { 368 ret := i + n 369 v.Val = ret 370 c.member[k] = v 371 c.Unlock() 372 return ret, nil 373 } 374 } 375 } 376 377 // IncrementInt64 为k对应的value增加n n必须为int64类型 378 func (c *cache) IncrementInt64(k string, n int64) (int64, error) { 379 c.Lock() 380 if v, ok := c.member[k]; !ok { 381 c.Unlock() 382 return 0, CacheNoExist 383 } else { 384 if v.Expired() { 385 c.Unlock() 386 c.Delete(k) 387 return 0, CacheExpire 388 } 389 if i, ok := v.Val.(int64); !ok { 390 c.Unlock() 391 return 0, CacheTypeErr 392 } else { 393 ret := i + n 394 v.Val = ret 395 c.member[k] = v 396 c.Unlock() 397 return ret, nil 398 } 399 } 400 } 401 402 // IncrementUint 为k对应的value增加n n必须为uint类型 403 func (c *cache) IncrementUint(k string, n uint) (uint, error) { 404 c.Lock() 405 if v, ok := c.member[k]; !ok { 406 c.Unlock() 407 return 0, CacheNoExist 408 } else { 409 if v.Expired() { 410 c.Unlock() 411 c.Delete(k) 412 return 0, CacheExpire 413 } 414 if i, ok := v.Val.(uint); !ok { 415 c.Unlock() 416 return 0, CacheTypeErr 417 } else { 418 ret := i + n 419 v.Val = ret 420 c.member[k] = v 421 c.Unlock() 422 return ret, nil 423 } 424 } 425 } 426 427 // IncrementUint8 为k对应的value增加n n必须为uint8类型 428 func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) { 429 c.Lock() 430 if v, ok := c.member[k]; !ok { 431 c.Unlock() 432 return 0, CacheNoExist 433 } else { 434 if v.Expired() { 435 c.Unlock() 436 c.Delete(k) 437 return 0, CacheExpire 438 } 439 if i, ok := v.Val.(uint8); !ok { 440 c.Unlock() 441 return 0, CacheTypeErr 442 } else { 443 ret := i + n 444 v.Val = ret 445 c.member[k] = v 446 c.Unlock() 447 return ret, nil 448 } 449 } 450 } 451 452 // IncrementUint16 为k对应的value增加n n必须为uint16类型 453 func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) { 454 c.Lock() 455 if v, ok := c.member[k]; !ok { 456 c.Unlock() 457 return 0, CacheNoExist 458 } else { 459 if v.Expired() { 460 c.Unlock() 461 c.Delete(k) 462 return 0, CacheExpire 463 } 464 if i, ok := v.Val.(uint16); !ok { 465 c.Unlock() 466 return 0, CacheTypeErr 467 } else { 468 ret := i + n 469 v.Val = ret 470 c.member[k] = v 471 c.Unlock() 472 return ret, nil 473 } 474 } 475 } 476 477 // IncrementUint32 为k对应的value增加n n必须为uint32类型 478 func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) { 479 c.Lock() 480 if v, ok := c.member[k]; !ok { 481 c.Unlock() 482 return 0, CacheNoExist 483 } else { 484 if v.Expired() { 485 c.Unlock() 486 c.Delete(k) 487 return 0, CacheExpire 488 } 489 if i, ok := v.Val.(uint32); !ok { 490 c.Unlock() 491 return 0, CacheTypeErr 492 } else { 493 ret := i + n 494 v.Val = ret 495 c.member[k] = v 496 c.Unlock() 497 return ret, nil 498 } 499 } 500 } 501 502 // IncrementUint64 为k对应的value增加n n必须为uint64类型 503 func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) { 504 c.Lock() 505 if v, ok := c.member[k]; !ok { 506 c.Unlock() 507 return 0, CacheNoExist 508 } else { 509 if v.Expired() { 510 c.Unlock() 511 c.Delete(k) 512 return 0, CacheExpire 513 } 514 if i, ok := v.Val.(uint64); !ok { 515 c.Unlock() 516 return 0, CacheTypeErr 517 } else { 518 ret := i + n 519 v.Val = ret 520 c.member[k] = v 521 c.Unlock() 522 return ret, nil 523 } 524 } 525 } 526 527 // IncrementUintPtr 为k对应的value增加n n必须为uintptr类型 528 func (c *cache) IncrementUintPtr(k string, n uintptr) (uintptr, error) { 529 c.Lock() 530 if v, ok := c.member[k]; !ok { 531 c.Unlock() 532 return 0, CacheNoExist 533 } else { 534 if v.Expired() { 535 c.Unlock() 536 c.Delete(k) 537 return 0, CacheExpire 538 } 539 if i, ok := v.Val.(uintptr); !ok { 540 c.Unlock() 541 return 0, CacheTypeErr 542 } else { 543 ret := i + n 544 v.Val = ret 545 c.member[k] = v 546 c.Unlock() 547 return ret, nil 548 } 549 } 550 } 551 552 // IncrementFloat32 为k对应的value增加n n必须为float32类型 553 func (c *cache) IncrementFloat32(k string, n float32) (float32, error) { 554 c.Lock() 555 if v, ok := c.member[k]; !ok { 556 c.Unlock() 557 return 0, CacheNoExist 558 } else { 559 if v.Expired() { 560 c.Unlock() 561 c.Delete(k) 562 return 0, CacheExpire 563 } 564 if i, ok := v.Val.(float32); !ok { 565 c.Unlock() 566 return 0, CacheTypeErr 567 } else { 568 ret := i + n 569 v.Val = ret 570 c.member[k] = v 571 c.Unlock() 572 return ret, nil 573 } 574 } 575 } 576 577 // IncrementFloat64 为k对应的value增加n n必须为float64类型 578 func (c *cache) IncrementFloat64(k string, n float64) (float64, error) { 579 c.Lock() 580 if v, ok := c.member[k]; !ok { 581 c.Unlock() 582 return 0, CacheNoExist 583 } else { 584 if v.Expired() { 585 c.Unlock() 586 c.Delete(k) 587 return 0, CacheExpire 588 } 589 if i, ok := v.Val.(float64); !ok { 590 c.Unlock() 591 return 0, CacheTypeErr 592 } else { 593 ret := i + n 594 v.Val = ret 595 c.member[k] = v 596 c.Unlock() 597 return ret, nil 598 } 599 } 600 } 601 602 // Decrement 为k对应的value减少n n必须为数字类型 603 func (c *cache) Decrement(k string, n int64) error { 604 c.Lock() 605 if v, ok := c.member[k]; !ok { 606 c.Unlock() 607 return CacheNoExist 608 } else { 609 if v.Expired() { 610 c.Unlock() 611 c.Delete(k) 612 return CacheExpire 613 } 614 switch v.Val.(type) { 615 case int: 616 v.Val = v.Val.(int) - int(n) 617 case int8: 618 v.Val = v.Val.(int8) - int8(n) 619 case int16: 620 v.Val = v.Val.(int16) - int16(n) 621 case int32: 622 v.Val = v.Val.(int32) - int32(n) 623 case int64: 624 v.Val = v.Val.(int64) - n 625 case uint: 626 v.Val = v.Val.(uint) - uint(n) 627 case uintptr: 628 v.Val = v.Val.(uintptr) - uintptr(n) 629 case uint8: 630 v.Val = v.Val.(uint8) - uint8(n) 631 case uint16: 632 v.Val = v.Val.(uint16) - uint16(n) 633 case uint32: 634 v.Val = v.Val.(uint32) - uint32(n) 635 case uint64: 636 v.Val = v.Val.(uint64) - uint64(n) 637 case float32: 638 v.Val = v.Val.(float32) - float32(n) 639 case float64: 640 v.Val = v.Val.(float64) - float64(n) 641 default: 642 c.Unlock() 643 return CacheTypeErr 644 } 645 c.member[k] = v 646 c.Unlock() 647 return nil 648 } 649 } 650 651 // DecrementFloat 为k对应的value减少n n必须为浮点数类型 652 func (c *cache) DecrementFloat(k string, n float64) error { 653 c.Lock() 654 if v, ok := c.member[k]; !ok { 655 c.Unlock() 656 return CacheNoExist 657 } else { 658 if v.Expired() { 659 c.Unlock() 660 c.Delete(k) 661 return CacheExpire 662 } 663 switch v.Val.(type) { 664 case float32: 665 v.Val = v.Val.(float32) - float32(n) 666 case float64: 667 v.Val = v.Val.(float64) - n 668 default: 669 c.Unlock() 670 return CacheTypeErr 671 } 672 c.member[k] = v 673 c.Unlock() 674 return nil 675 } 676 } 677 678 // DecrementInt 为k对应的value减少n n必须为int类型 679 func (c *cache) DecrementInt(k string, n int) (int, error) { 680 c.Lock() 681 if v, ok := c.member[k]; !ok { 682 c.Unlock() 683 return 0, CacheNoExist 684 } else { 685 if v.Expired() { 686 c.Unlock() 687 c.Delete(k) 688 return 0, CacheExpire 689 } 690 if i, ok := v.Val.(int); !ok { 691 c.Unlock() 692 return 0, CacheTypeErr 693 } else { 694 ret := i - n 695 v.Val = ret 696 c.member[k] = v 697 c.Unlock() 698 return ret, nil 699 } 700 } 701 } 702 703 // DecrementInt8 为k对应的value减少n n必须为int8类型 704 func (c *cache) DecrementInt8(k string, n int8) (int8, error) { 705 c.Lock() 706 if v, ok := c.member[k]; !ok { 707 c.Unlock() 708 return 0, CacheNoExist 709 } else { 710 if v.Expired() { 711 c.Unlock() 712 c.Delete(k) 713 return 0, CacheExpire 714 } 715 if i, ok := v.Val.(int8); !ok { 716 c.Unlock() 717 return 0, CacheTypeErr 718 } else { 719 ret := i - n 720 v.Val = ret 721 c.member[k] = v 722 c.Unlock() 723 return ret, nil 724 } 725 } 726 } 727 728 // DecrementInt16 为k对应的value减少n n必须为int16类型 729 func (c *cache) DecrementInt16(k string, n int16) (int16, error) { 730 c.Lock() 731 if v, ok := c.member[k]; !ok { 732 c.Unlock() 733 return 0, CacheNoExist 734 } else { 735 if v.Expired() { 736 c.Unlock() 737 c.Delete(k) 738 return 0, CacheExpire 739 } 740 if i, ok := v.Val.(int16); !ok { 741 c.Unlock() 742 return 0, CacheTypeErr 743 } else { 744 ret := i - n 745 v.Val = ret 746 c.member[k] = v 747 c.Unlock() 748 return ret, nil 749 } 750 } 751 } 752 753 // DecrementInt32 为k对应的value减少n n必须为int32类型 754 func (c *cache) DecrementInt32(k string, n int32) (int32, error) { 755 c.Lock() 756 if v, ok := c.member[k]; !ok { 757 c.Unlock() 758 return 0, CacheNoExist 759 } else { 760 if v.Expired() { 761 c.Unlock() 762 c.Delete(k) 763 return 0, CacheExpire 764 } 765 if i, ok := v.Val.(int32); !ok { 766 c.Unlock() 767 return 0, CacheTypeErr 768 } else { 769 ret := i - n 770 v.Val = ret 771 c.member[k] = v 772 c.Unlock() 773 return ret, nil 774 } 775 } 776 } 777 778 // DecrementInt64 为k对应的value减少n n必须为int64类型 779 func (c *cache) DecrementInt64(k string, n int64) (int64, error) { 780 c.Lock() 781 if v, ok := c.member[k]; !ok { 782 c.Unlock() 783 return 0, CacheNoExist 784 } else { 785 if v.Expired() { 786 c.Unlock() 787 c.Delete(k) 788 return 0, CacheExpire 789 } 790 if i, ok := v.Val.(int64); !ok { 791 c.Unlock() 792 return 0, CacheTypeErr 793 } else { 794 ret := i - n 795 v.Val = ret 796 c.member[k] = v 797 c.Unlock() 798 return ret, nil 799 } 800 } 801 } 802 803 // DecrementUint 为k对应的value减少n n必须为uint类型 804 func (c *cache) DecrementUint(k string, n uint) (uint, error) { 805 c.Lock() 806 if v, ok := c.member[k]; !ok { 807 c.Unlock() 808 return 0, CacheNoExist 809 } else { 810 if v.Expired() { 811 c.Unlock() 812 c.Delete(k) 813 return 0, CacheExpire 814 } 815 if i, ok := v.Val.(uint); !ok { 816 c.Unlock() 817 return 0, CacheTypeErr 818 } else { 819 ret := i - n 820 v.Val = ret 821 c.member[k] = v 822 c.Unlock() 823 return ret, nil 824 } 825 } 826 } 827 828 // DecrementUint8 为k对应的value减少n n必须为uint8类型 829 func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) { 830 c.Lock() 831 if v, ok := c.member[k]; !ok { 832 c.Unlock() 833 return 0, CacheNoExist 834 } else { 835 if v.Expired() { 836 c.Unlock() 837 c.Delete(k) 838 return 0, CacheExpire 839 } 840 if i, ok := v.Val.(uint8); !ok { 841 c.Unlock() 842 return 0, CacheTypeErr 843 } else { 844 ret := i - n 845 v.Val = ret 846 c.member[k] = v 847 c.Unlock() 848 return ret, nil 849 } 850 } 851 } 852 853 // DecrementUint16 为k对应的value减少n n必须为uint16类型 854 func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) { 855 c.Lock() 856 if v, ok := c.member[k]; !ok { 857 c.Unlock() 858 return 0, CacheNoExist 859 } else { 860 if v.Expired() { 861 c.Unlock() 862 c.Delete(k) 863 return 0, CacheExpire 864 } 865 if i, ok := v.Val.(uint16); !ok { 866 c.Unlock() 867 return 0, CacheTypeErr 868 } else { 869 ret := i - n 870 v.Val = ret 871 c.member[k] = v 872 c.Unlock() 873 return ret, nil 874 } 875 } 876 } 877 878 // DecrementUint32 为k对应的value减少n n必须为uint32类型 879 func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) { 880 c.Lock() 881 if v, ok := c.member[k]; !ok { 882 c.Unlock() 883 return 0, CacheNoExist 884 } else { 885 if v.Expired() { 886 c.Unlock() 887 c.Delete(k) 888 return 0, CacheExpire 889 } 890 if i, ok := v.Val.(uint32); !ok { 891 c.Unlock() 892 return 0, CacheTypeErr 893 } else { 894 ret := i - n 895 v.Val = ret 896 c.member[k] = v 897 c.Unlock() 898 return ret, nil 899 } 900 } 901 } 902 903 // DecrementUint64 为k对应的value减少n n必须为uint64类型 904 func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) { 905 c.Lock() 906 if v, ok := c.member[k]; !ok { 907 c.Unlock() 908 return 0, CacheNoExist 909 } else { 910 if v.Expired() { 911 c.Unlock() 912 c.Delete(k) 913 return 0, CacheExpire 914 } 915 if i, ok := v.Val.(uint64); !ok { 916 c.Unlock() 917 return 0, CacheTypeErr 918 } else { 919 ret := i - n 920 v.Val = ret 921 c.member[k] = v 922 c.Unlock() 923 return ret, nil 924 } 925 } 926 } 927 928 // DecrementUintPtr 为k对应的value减少n n必须为uintptr类型 929 func (c *cache) DecrementUintPtr(k string, n uintptr) (uintptr, error) { 930 c.Lock() 931 if v, ok := c.member[k]; !ok { 932 c.Unlock() 933 return 0, CacheNoExist 934 } else { 935 if v.Expired() { 936 c.Unlock() 937 c.Delete(k) 938 return 0, CacheExpire 939 } 940 if i, ok := v.Val.(uintptr); !ok { 941 c.Unlock() 942 return 0, CacheTypeErr 943 } else { 944 ret := i - n 945 v.Val = ret 946 c.member[k] = v 947 c.Unlock() 948 return ret, nil 949 } 950 } 951 } 952 953 // DecrementFloat32 为k对应的value减少n n必须为float32类型 954 func (c *cache) DecrementFloat32(k string, n float32) (float32, error) { 955 c.Lock() 956 if v, ok := c.member[k]; !ok { 957 c.Unlock() 958 return 0, CacheNoExist 959 } else { 960 if v.Expired() { 961 c.Unlock() 962 c.Delete(k) 963 return 0, CacheExpire 964 } 965 if i, ok := v.Val.(float32); !ok { 966 c.Unlock() 967 return 0, CacheTypeErr 968 } else { 969 ret := i - n 970 v.Val = ret 971 c.member[k] = v 972 c.Unlock() 973 return ret, nil 974 } 975 } 976 } 977 978 // DecrementFloat64 为k对应的value减少n n必须为float64类型 979 func (c *cache) DecrementFloat64(k string, n float64) (float64, error) { 980 c.Lock() 981 if v, ok := c.member[k]; !ok { 982 c.Unlock() 983 return 0, CacheNoExist 984 } else { 985 if v.Expired() { 986 c.Unlock() 987 c.Delete(k) 988 return 0, CacheExpire 989 } 990 if i, ok := v.Val.(float64); !ok { 991 c.Unlock() 992 return 0, CacheTypeErr 993 } else { 994 ret := i - n 995 v.Val = ret 996 c.member[k] = v 997 c.Unlock() 998 return ret, nil 999 } 1000 } 1001 } 1002 1003 // Delete 删除k的cache 如果 capture != nil 会调用 capture 函数 将 kv传入 1004 func (c *cache) Delete(k string) { 1005 c.Lock() 1006 v, ok := c.delete(k) 1007 c.Unlock() 1008 if ok { 1009 c.capture(k, v) 1010 } 1011 } 1012 1013 func (c *cache) _delete(k string) { 1014 v, ok := c.delete(k) 1015 if ok { 1016 c.capture(k, v) 1017 } 1018 } 1019 1020 // delete 删除k的cache 如果具有 capture != nil 则会携带v返回 1021 func (c *cache) delete(k string) (interface{}, bool) { 1022 if c.capture != nil { 1023 if v, ok := c.member[k]; ok { 1024 delete(c.member, k) 1025 return v.Val, true 1026 } 1027 } 1028 delete(c.member, k) 1029 return nil, false 1030 } 1031 1032 // DeleteExpire 删除已经过期的kv 1033 func (c *cache) DeleteExpire() { 1034 var kvList []kv 1035 if c.capture != nil { 1036 kvList = make([]kv, 0, len(c.member)/4) 1037 } 1038 c.Lock() 1039 t := time.Now().UnixNano() 1040 for k, v := range c.member { 1041 if v.Expired(t) { 1042 if vv, ok := c.delete(k); ok && c.capture != nil { 1043 kvList = append(kvList, kv{k, vv}) 1044 } 1045 } 1046 } 1047 c.Unlock() 1048 for _, v := range kvList { 1049 c.capture(v.key, v.value) 1050 } 1051 } 1052 1053 // ChangeCapture 替换cache中capture的处理函数 1054 func (c *cache) ChangeCapture(f func(string, interface{})) { 1055 c.Lock() 1056 c.capture = f 1057 c.Unlock() 1058 } 1059 1060 // Save 将 c.member 写入到 w 中 1061 func (c *cache) Save(w io.Writer) (err error) { 1062 enc := gob.NewEncoder(w) 1063 defer func() { 1064 if e := recover(); e != nil { 1065 err = CacheGobErr 1066 } 1067 }() 1068 c.Lock() 1069 defer c.Unlock() 1070 for _, iterator := range c.member { 1071 gob.Register(iterator.Val) 1072 } 1073 return enc.Encode(&c.member) 1074 } 1075 1076 // SaveFile 将 c.member 保存到 path 中 1077 func (c *cache) SaveFile(path string) error { 1078 f, err := os.Create(path) 1079 if err != nil { 1080 return err 1081 } 1082 defer f.Close() 1083 return c.Save(f) 1084 } 1085 1086 // Load 从r 中加载 c.member 1087 func (c *cache) Load(r io.Reader) error { 1088 dec := gob.NewDecoder(r) 1089 member := map[string]Iterator{} 1090 if err := dec.Decode(&member); err != nil { 1091 return err 1092 } else { 1093 c.Lock() 1094 for k, iterator := range member { 1095 if v, ok := c.member[k]; !ok || v.Expired() { 1096 c.member[k] = iterator 1097 } 1098 } 1099 c.Unlock() 1100 } 1101 return nil 1102 } 1103 1104 // LoadFile 从 path 中加载 c.member 1105 func (c *cache) LoadFile(path string) error { 1106 f, err := os.Open(path) 1107 if err != nil { 1108 return err 1109 } 1110 defer f.Close() 1111 return c.Load(f) 1112 } 1113 1114 // Iterator 返回 cache 中所有有效的对象 1115 func (c *cache) Iterator() map[string]Iterator { 1116 c.RLock() 1117 1118 ret := make(map[string]Iterator, len(c.member)) 1119 keys := make([]string, 0, 10) 1120 for k, v := range c.member { 1121 if !v.Expired() { 1122 ret[k] = v 1123 } else { 1124 keys = append(keys, k) 1125 } 1126 } 1127 c.RUnlock() 1128 // 清除过期key 1129 for _, key := range keys { 1130 c.Delete(key) 1131 } 1132 return ret 1133 } 1134 1135 // Count 计算现在 member 中 kv的数量 (所有) 1136 func (c *cache) Count() int { 1137 c.RLock() 1138 defer c.RUnlock() 1139 return len(c.member) 1140 } 1141 1142 // Flush 释放member成员 1143 func (c *cache) Flush() { 1144 c.Lock() 1145 defer c.Unlock() 1146 c.member = make(map[string]Iterator) 1147 } 1148 1149 func (c *cache) Shutdown() error { 1150 c.Flush() 1151 c.cancel() 1152 return nil 1153 }