With both port and sherry it depends what kind you’re drinking. Tawny or liquor port will keep for pretty long periods. Vintage port is best drunk at a sitting.
Fino and manzanilla sherries (the pale, dry ones) shouldn’t be kept unopened for more than a couple of months. Once open, a couple of weeks in the fridge is about all they can take.