Spring mvc 是否可以在mybatis中为每个用户设置唯一的会话缓存 @Autowired @限定符(“dashbSqlSessionFactory”) 私有SqlSessionFactoryBean dashbSqlSessionFactory; @RequestMapping(value=“/clearcache”,method=RequestMethod.POST) public@ResponseBody void clearCache()引发异常{ //System.out.println(“缓存已清除……”); 配置=dashbSqlSessionFactory.getObject().getConfiguration(); Collection caches=configuration.getCaches(); 对于(缓存:缓存){ /*System.out.println(“缓存名称:“+cache”); cache.removeObject(“KeyEquipmentShiftaTomatedModelData”)*/ /*锁w=cache.getReadWriteLock().writeLock(); w、 锁(); 试试{*/ cache.clear(); /*}最后{ w、 解锁(); }*/ } }

Spring mvc 是否可以在mybatis中为每个用户设置唯一的会话缓存 @Autowired @限定符(“dashbSqlSessionFactory”) 私有SqlSessionFactoryBean dashbSqlSessionFactory; @RequestMapping(value=“/clearcache”,method=RequestMethod.POST) public@ResponseBody void clearCache()引发异常{ //System.out.println(“缓存已清除……”); 配置=dashbSqlSessionFactory.getObject().getConfiguration(); Collection caches=configuration.getCaches(); 对于(缓存:缓存){ /*System.out.println(“缓存名称:“+cache”); cache.removeObject(“KeyEquipmentShiftaTomatedModelData”)*/ /*锁w=cache.getReadWriteLock().writeLock(); w、 锁(); 试试{*/ cache.clear(); /*}最后{ w、 解锁(); }*/ } },spring-mvc,caching,mybatis,ibatis,spring-mybatis,Spring Mvc,Caching,Mybatis,Ibatis,Spring Mybatis,在Mybatis mapper xml中 @Autowired @Qualifier("dashbSqlSessionFactory") private SqlSessionFactoryBean dashbSqlSessionFactory; @RequestMapping(value = "/clearcache", method = RequestMethod.POST) public @ResponseBody void clearCach

在Mybatis mapper xml中

@Autowired
       @Qualifier("dashbSqlSessionFactory")
       private SqlSessionFactoryBean  dashbSqlSessionFactory;

@RequestMapping(value = "/clearcache", method = RequestMethod.POST)
        public @ResponseBody void clearCache() throws Exception{
            //System.out.println("Cache is cleared.................");
             Configuration configuration = dashbSqlSessionFactory.getObject().getConfiguration(); 
                Collection<Cache> caches = configuration.getCaches(); 
                 for (Cache cache : caches) { 
                  /*System.out.println("cache Name:   "+cache);
                  cache.removeObject("keyEquipmentShiftAutomatedModelData");*/
                     /*Lock w = cache.getReadWriteLock().writeLock(); 
                     w.lock(); 
                     try { */
                         cache.clear(); 
                     /*} finally { 
                         w.unlock(); 
                     }*/
            }
            }

根据上面的代码,我希望缓存处于会话级别,并且对单个用户是唯一的,但是当其他用户登录到系统时,我的缓存将被覆盖。如何使两者具有不同的会话
dashbSqlSessionFactory.getObject().getConfiguration();
Collection caches=configuration.getCaches()

根据我的理解,不应该在其他会话中获取用户的缓存。我的代码有什么问题吗?

好吧,既然
SqlSessionFactoryBean
已经构建了配置,请参见
buildSqlSessionFactory()
,并且似乎只有一个
SqlSessionFactory
实例,我假设
配置在所有
缓存中共享。您可能可以实现自己的
缓存
类,该类以某种方式检索当前会话,并且仅对该会话的数据进行操作……我认为@FlorianSchaetz的做法是正确的。另外,请记住@Autowired创建了一个单例。因此,SqlSessionFactoryBean只有一个实例。有趣的一点是,如果您将
SqlSessionFactoryBean
的范围更改为
session
,则会发生什么。但是,即使它可以工作,它也可能是减慢整个应用程序速度的一个非常好的方法。
<cache
    eviction="FIFO"
    size="512"
    readOnly="true"/>