jmdns: JmDNS should provide an API to invalidate the cache

I have a situation where I need to check for services currently available in the network. These services may come and go so I need to recheck, either periodically or when triggered by the user.

However I have found that after JmDNS has discovered a service, it will keep returning cached information regardless of whether the device is still present in the network. I have tried unregistering the listener and registering it again, but when I do this serviceAdded is immediately called with stale (cached) information.

Is there any way to force JmDNS to drop any cached information so that I can start a “fresh” search, other than calling JmDNS.close()? (this works, but it takes a few seconds to complete).

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 1
  • Comments: 18 (4 by maintainers)

Most upvoted comments

@ViToni This issue was actually about the need to have a way to trigger a “re-check”, discarding any cached information. This was even more necessary due to #18, which is now fixed with your changes (#126), but there are other cases, see comment from @namannik:

If a service on the network shuts down gracefully, it broadcasts on the network that it’s going away. On iOS, this correctly causes the service to be removed from the list immediately, but JmDNS seems to hold on to the service.

(this was #18, now fixed)

This is different than the caching mentioned by @spearway . The cache retains items that were unexpectedly removed from the network. (i.e. The network cable was unplugged.)

Due to the above I need a way to force a re-check discarding any items that may still be cached. Currently the only ways I have found to do that are:

  • Closing and reopening JmDNS, which works but it is slow (see #82)
  • Calling ((JmDNSImpl) jmdns).getCache().clear(), which is not a public API, but works fine.