RPOPLPUSH removes the last element from one list and pushes it to another list. If the source list is empty, an error is returned. If the destination list is empty, it is created. Atomicity is not guaranteed.
(source string, destination string, ttl int64)
| 464 | // If the destination list is empty, it is created. |
| 465 | // Atomicity is not guaranteed. |
| 466 | func (l *ListStructure) RPOPLPUSH(source string, destination string, ttl int64) error { |
| 467 | // Get the source list |
| 468 | lst1, _, err := l.getListFromDB(source, false) |
| 469 | if err != nil { |
| 470 | return err |
| 471 | } |
| 472 | |
| 473 | // Get the destination list |
| 474 | lst2, _, err := l.getListFromDB(destination, true) |
| 475 | if err != nil { |
| 476 | return err |
| 477 | } |
| 478 | var expirationTime time.Duration |
| 479 | // Return error if the source list is empty |
| 480 | if lst1.Length == 0 { |
| 481 | return ErrListEmpty |
| 482 | } |
| 483 | |
| 484 | // Find the last node of the source list |
| 485 | lastNode := lst1.Head |
| 486 | prevNode := lst1.Head |
| 487 | for lastNode.Next != nil { |
| 488 | prevNode = lastNode |
| 489 | lastNode = lastNode.Next |
| 490 | } |
| 491 | |
| 492 | // Remove the last node from the source list |
| 493 | if lst1.Length == 1 { |
| 494 | lst1.Head = nil |
| 495 | } else { |
| 496 | prevNode.Next = nil |
| 497 | } |
| 498 | lst1.Length-- |
| 499 | |
| 500 | // Add the last node to the head of the destination list |
| 501 | lastNode.Next = lst2.Head |
| 502 | lst2.Head = lastNode |
| 503 | lst2.Length++ |
| 504 | if ttl > 0 { |
| 505 | expirationTime = time.Duration(ttl) * time.Second |
| 506 | } |
| 507 | // Store in the database |
| 508 | err = l.setListToDB(source, lst1, expirationTime) |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | return l.setListToDB(destination, lst2, expirationTime) |
| 513 | } |
| 514 | |
| 515 | func (l *ListStructure) TTL(k string) (int64, error) { |
| 516 | _, expire, err := l.getListFromDB(k, false) |