Optimized runtime of getAllConnectedIdentifiers and added caching for results