Using Redis as a cache storage for for multiple application on the same server

末鹿安然 提交于 2019-12-05 14:36:08

问题


I want to use Redis as a cache storage for multiple applications on the same physical machine.

I know at least two ways of doing it:

  1. by running several Redis instances on different ports;
  2. by using different Redis databases for different applications.

But I don't know which one is better for me.

What are advantages and disadvantages of these methods?

Is there any better way of doing it?


回答1:


Generally, you should prefer the 1st approach, i.e. dedicated Redis servers. Shared databases are managed by the same Redis process and can therefore block each other. Additionally, shared databases share the same configuration (although in your case this may not be an issue since all databases are intended for caching). Lastly, shared databases are not supported by Redis Cluster.

For more information refer to this blog post: https://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances




回答2:


You can use different cache manager for each application will also work same way I am using. like :

@Bean(name = "myCacheManager")
    public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        return cacheManager;
    }

    @Bean(name ="customKeyGenerator")
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                // This will generate a unique key of the class name, the method name,
                // and all method parameters appended.
                StringBuilder sb = new StringBuilder();
                sb.append(o.getClass().getName());
                sb.append(method.getName());
                for (Object obj : objects) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }


来源:https://stackoverflow.com/questions/27217502/using-redis-as-a-cache-storage-for-for-multiple-application-on-the-same-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!