草庐IT

spring - Redis 与 Guava 缓存

coder 2023-07-17 原文

我有一段代码,其中实现了缓存机制。 以前是基于 Guava 的缓存,现在考虑到集中缓存的需要,我正在转向 Redis。

但我担心它的性能,因为我发现与 guave 相比,redis 的性能非常低。

我已经测量了从缓存中获取类对象的 api 的性能 在 Guava 中是 5 毫秒,而在 Redis 中是 200 毫秒。
这是负载测试情况下的平均响应,如果单个请求响应差别不大。
我已经使用缓存抽象实现了 Spring 数据 Redis。

以下是示例 Redis 配置:

 @Bean
 public RedisConnectionFactory redisConnectionFactory(@Value("${redis.host}") String redisHost,
            @Value("${redis.port}") Integer redisPort) {
        JedisConnectionFactory cf = new JedisConnectionFactory();
        cf.setHostName(redisHost);
        cf.setPort(redisPort);
        cf.setUsePool(true);
        JedisPoolConfig jedisPool = new JedisPoolConfig();
        jedisPool.setMaxTotal(500);
        cf.setPoolConfig(jedisPool);
        return cf;
    }

    @Bean(name = "redisTemplate")
    RedisTemplate<Object,Object> redisTemplate() {
        final RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
        template.setConnectionFactory(applicationContext.getBean(RedisConnectionFactory.class));
        return template;
    }

    @Bean
    public CacheManager cacheManager() {
        if(isRedisEnabled)
        {
            RedisTemplate<?,?> template = (RedisTemplate<?, ?>) applicationContext.getBean("redisTemplate");
            RedisCacheManager redisCacheManager = new PieRedisCacheManager(template);
            redisCacheManager.setUsePrefix(true);
           
                try
                {
                    template.getConnectionFactory().getConnection();
                }
                catch(Exception e)
                {
                    LOG.error("Unable to connect to redis Server ,closing application : "+e);
                    SpringApplication.exit(applicationContext);
                }
            return redisCacheManager;
        }
        else
        {
            GuavaCacheManager guavaCacheManager = new GuavaCacheManager();
            guavaCacheManager.setCacheBuilder(CacheBuilder.newBuilder());
            return guavaCacheManager;
        }
    }

除此之外,对于 redis 服务器配置,我尝试禁用所有持久性,因为我不需要它。 但性能仍然很低。

我的主要问题是,是配置导致了这种情况,还是 Redis 与 Guava 相比性能非常低?
通过更多的配置调优,redis 的性能能否与 guava 相媲美?
请建议。

最佳答案

免责声明:我绝不是使用 Guava 或 Redis 的专家,尽管我都使用过。

明显的性能损失是显而易见的

对于初学者来说,在我看来,从 Guava 切换到 Redis 时遇到性能下降是完全正常的。

主要是因为:

  • Guava 提供内存中的缓存,并且对于您的应用程序运行的 JVM 是本地的。因此,您的应用程序无需借助任何进程间通信即可轻松查询。

  • Redis 是一个独立的键值存储应用程序,在自己的进程中运行。因此,您必须以某种方式与其通信以建立连接并发送请求。

因此,即使你们在同一台机器上,即使 Redis 的固有性能优于 Guava 的缓存(老实说,在一般情况下可能就是这种情况),无论如何你肯定会看到性能下降。

可能的改进

也就是说,您可以通过配置和架构选择来提高性能:

  • 确保使用本地 IP 连接到 Redis。这将有助于避免在尝试建立连接时解析任何地址。

  • 确保通过尽可能轻量级的协议(protocol)连接到 Redis。正如我假设您使用的是本地 Redis 服务器,并且您遵守前一点,您将不需要任何附加功能、安全协议(protocol)等...

  • 可能适用于您的场景的任何其他常见 Redis 配置调整。

关于spring - Redis 与 Guava 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32075954/

有关spring - Redis 与 Guava 缓存的更多相关文章

  1. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  2. ruby - 如何在 Ubuntu 中清除 Ruby Phusion Passenger 的缓存? - 2

    我试过重新启动apache,缓存的页面仍然出现,所以一定有一个文件夹在某个地方。我没有“公共(public)/缓存”,那么我还应该查看哪些其他地方?是否有一个URL标志也可以触发此效果? 最佳答案 您需要触摸一个文件才能清除phusion,例如:touch/webapps/mycook/tmp/restart.txt参见docs 关于ruby-如何在Ubuntu中清除RubyPhusionPassenger的缓存?,我们在StackOverflow上找到一个类似的问题:

  3. ruby-on-rails - Ruby on Rails 计数器缓存错误 - 2

    尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot

  4. spring.profiles.active和spring.profiles.include的使用及区别说明 - 2

    转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev

  5. ruby-on-rails - Spring 不起作用。 [未初始化常量 Spring::SID::DL] - 2

    我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li

  6. 【云原生】SpringCloud-Spring Boot Starter使用测试 - 2

    目录SpringBootStarter是什么?以前传统的做法使用SpringBootStarter之后starter的理念:starter的实现: 创建SpringBootStarter步骤在idea新建一个starter项目、直接执行下一步即可生成项目。 在xml中加入如下配置文件:创建proterties类来保存配置信息创建业务类:创建AutoConfiguration测试如下:SpringBootStarter是什么? SpringBootStarter是在SpringBoot组件中被提出来的一种概念、简化了很多烦琐的配置、通过引入各种SpringBootStarter包可以快速搭建出一

  7. ruby-on-rails - bundle 安装尝试使用缓存文件 - 2

    当我尝试进行bundle安装时,我的gem_path和gem_home指向/usr/local/rvm/gems/我没有写入权限,并且由于权限无效而失败。因此,我已将两个路径都更改为我具有写入权限的本地目录。这样做时,我进行了bundle安装,我得到:bruno@test6:~$bundleinstallFetchinggemmetadatafromhttps://rubygems.org/.........Fetchinggemmetadatafromhttps://rubygems.org/..Bundler::GemspecError:Couldnotreadgemat/afs/

  8. ruby-on-rails - Heroku Action 缓存似乎不起作用 - 2

    我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错

  9. ruby-on-rails - rails expire_page 没有删除缓存的文件 - 2

    我有一个具有页面缓存的ControllerAction,我制作了一个清扫程序,它使用Controller和指定的Action调用expire_page...Controller操作呈现一个js.erb模板,所以我试图确保expire_page删除public/javascripts中的.js文件,但它没有这样做。classJavascriptsController"javascripts",:action=>"lol",:format=>'js')endend...所以,我访问javascripts/lol.js并呈现我的模板。我验证了public/javascripts/lol.js

  10. ruby-on-rails - rails 3 缓存 : expire action for named route - 2

    我的Controller有这个:caches_action:render_ticker_for_channel,:expires_in=>30.seconds在我的路由文件中我有这个:match'/render_c_t/:channel_id'=>'render#render_ticker_for_channel',:as=>:render_channel_ticker在日志文件中我看到了这个:Writefragmentviews/mcr3.dev/render_c_t/63(11.6ms)我如何手动使它过期?我需要从与渲染Controller不同的Controller使它过期,但即使

随机推荐