I don't think Spring offers LDAP client-side caching out of the box, since caching the results of an LDAP query on the client poses a security risk. The cache will certainly store outdated data at some point, which is not a big problem if it is, for example, the user's email address / home address, but much worse when it comes to, for example, role assignments and other data, associated with authentication / authorization. You will be much better off by scaling the server side so that it can handle the load.
At the same time, introducing caching is quite simple, since Spring 3.1, because it provides excellent support for it. In your case, it is enough to use a custom LdapContextSource , as shown below:
public class CachingLdapContextSource extends AbstractContextSource { @Override protected DirContext getDirContextInstance(Hashtable environment) throws NamingException { InitialLdapContext context = new InitialLdapContext(environment, null); return new CachingDirContextWrapper(context); } }
The wrapper class simply delegates all the DirContext methods to the base implementation and decorates the methods cached with @Cacheable .
class CachingDirContextWrapper implements DirContext { private final DirContext delegate; CachingDirContextWrapper(DirContext delegate) { this.delegate = delegate; } @Override @Cacheable(value = "search") public NamingEnumeration<SearchResult> search(...) { return delegate.search(name, matchingAttributes, attributesToReturn); } ... }
Check out the official documentation and this tutorial on how to set up the cache storage that Spring will use.
But, again, you better not do this, I think.
zagyi source share