# Caching Custom Realm Plugin tokens

**URL:** <https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972>\
**Category:** Elasticsearch\
**Created:** [August 24, 2018, 9:01pm UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972 "2018-08-24T21:01:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gautu7](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@gautu7](https://discuss.elastic.co/u/gautu7)\
**Post date:** [August 24, 2018, 9:01pm UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972/1 "2018-08-24T21:01:27Z")

</div>

I have a custom realm plugin implemented to which I want to add caching until a certain time period and invalidate the cache. There is a CachingRealm interface which I can implement for the cache eviction APIs.  
I was looking at the example in the link below and I see that there is a different cache realm type defined and it's extending the custom realm, doing some cache lookups/insertion and calling the super custom realm methods. I feel like this is some kind of duplication and could have the custom realm class itself implement the CachingRealm and have the logic there. Is there any specific reason it's done this way in the example? Also, why is the caching type not mapped in the .yml file?

> <https://github.com/elastic/shield-custom-realm-example/blob/6.2/src/main/java/org/elasticsearch/example/realm/CustomCachingRealm.java>

Also, wanted to confirm if elastic is adding some kind of built-in caching for custom realm types like it does for some configured realms, I presume it's not.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 27, 2018, 6:16am UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972/2 "2018-08-27T06:16:52Z")

</div>

Hey,

the different type was just implemented as an example (you should only have one of those in your plugin and that should the caching). You are good to go to implement the `CachingRealm` interface and yes, you have to implement the caching yourself (the example shows a time based expiry, but you can either tweak the (also other available) options of that one or just come up with your own implementations suiting your needs better). Check the `org.elasticsearch.common.cache.CacheBuilder` class for more info.

--Alex

---

<div class="post-metadata">

**Author:** ![gautu7](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@gautu7](https://discuss.elastic.co/u/gautu7)\
**Post date:** [August 27, 2018, 5:23pm UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972/3 "2018-08-27T17:23:36Z")

</div>

I went through the `org.elasticsearch.common.cache.CacheBuilder` implementation, looks like the `setExpireAfterAccess` and `setExpireAfterWrite` already throw out a Key after the specified amount of time, if that's the case why do we have this method to evict a Key from the Cache?  
/\*\*  
\* Removes the entry from the cache identified by the username  
\* @param username the identifier for the user to remove  
\*/  
@Override  
public void expire(String username) {  
cache.invalidate(username);  
}

If I just want to store my tokens in a cache for a certain period of time, looks like I don't even have to implement the `CachingRealm`, is my understanding right? I'm trying to think of a use case where this makes sense, is this for explicit removal of Key based on some custom logic?

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [August 28, 2018, 2:08am UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972/4 "2018-08-28T02:08:04Z")

</div>

If your realm caches information about users then it should implement `CachingRealm` so that the realm manager can clear the cache when it needs to.  
Mostly that's in response to the clear cache API, but it can be triggered on other cases.

---

<div class="post-metadata">

**Author:** ![gautu7](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@gautu7](https://discuss.elastic.co/u/gautu7)\
**Post date:** [August 30, 2018, 12:58am UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972/5 "2018-08-30T00:58:55Z")

</div>

One more thing I observed in the example is that the cache is not static, this would end up creating a new cache for every authorization call.

```
 private final Cache<String, UserHolder> cache = CacheBuilder.<String, UserHolder>builder()
            .setExpireAfterAccess(TimeValue.timeValueMinutes(30))
            .build();
```

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [August 30, 2018, 2:10am UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972/6 "2018-08-30T02:10:16Z")

</div>

Your _Realm_ is a singleton. There is 1 instance of your realm per ES node, so a non-static cache is fine.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 27, 2018, 2:21am UTC](https://discuss.elastic.co/t/caching-custom-realm-plugin-tokens/145972/7 "2018-09-27T02:21:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
