decodeList decodes the value
(value []byte)
| 673 | |
| 674 | // decodeList decodes the value |
| 675 | func (l *ListStructure) decodeList(value []byte) (*DecodedList, error) { |
| 676 | // Check the length of the value |
| 677 | if len(value) < 1 { |
| 678 | return nil, ErrInvalidValue |
| 679 | } |
| 680 | |
| 681 | // Check the type of the value |
| 682 | if value[0] != List { |
| 683 | return nil, ErrInvalidType |
| 684 | } |
| 685 | |
| 686 | // Create a bytes.Buffer from the value (excluding the first byte) |
| 687 | buffer := bytes.NewBuffer(value[1:]) |
| 688 | |
| 689 | // Create a new gob.Decoder |
| 690 | dec := gob.NewDecoder(buffer) |
| 691 | |
| 692 | // Create a new ExpiredItem to hold the decoded value and expiration time |
| 693 | var expiredItem ExpiredItem |
| 694 | |
| 695 | // Decode the ExpiredItem |
| 696 | err := dec.Decode(&expiredItem) |
| 697 | if err != nil { |
| 698 | return nil, err |
| 699 | } |
| 700 | |
| 701 | // Create a bytes.Buffer from the ExpiredItem's data |
| 702 | dataBuffer := bytes.NewBuffer(expiredItem.Data) |
| 703 | |
| 704 | // Create a new gob.Decoder for the data |
| 705 | dataDec := gob.NewDecoder(dataBuffer) |
| 706 | |
| 707 | // Create a new list to hold the decoded data |
| 708 | var lst list |
| 709 | |
| 710 | // Decode the data into the list |
| 711 | err = dataDec.Decode(&lst) |
| 712 | if err != nil { |
| 713 | return nil, err |
| 714 | } |
| 715 | |
| 716 | expiration := expiredItem.Expiration |
| 717 | |
| 718 | if expiration != 0 && expiration < time.Now().UnixNano() { |
| 719 | return nil, _const.ErrKeyIsExpired |
| 720 | } |
| 721 | |
| 722 | decodedList := &DecodedList{ |
| 723 | List: &lst, |
| 724 | Expiration: expiration, // Calculate remaining time |
| 725 | } |
| 726 | |
| 727 | return decodedList, nil |
| 728 | } |
| 729 | |
| 730 | func (s *ListStructure) Stop() error { |
| 731 | err := s.db.Close() |