com.j256.ormlite.dao.Dao.setObjectCache()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(157)

本文整理了Java中com.j256.ormlite.dao.Dao.setObjectCache()方法的一些代码示例,展示了Dao.setObjectCache()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Dao.setObjectCache()方法的具体详情如下:
包路径:com.j256.ormlite.dao.Dao
类名称:Dao
方法名:setObjectCache

Dao.setObjectCache介绍

[英]Same as #setObjectCache(boolean) except you specify the actual cache instance to use for the DAO. This allows you to use a ReferenceObjectCache with SoftReference setting, the LruObjectCache, or inject your own cache implementation. Call it with null to disable the cache.
[中]与#setObjectCache(布尔值)相同,只是指定要用于DAO的实际缓存实例。这允许您使用带有SoftReference设置的ReferenceObjectCache、LruObjectCache或注入您自己的缓存实现。使用null调用它以禁用缓存。

代码示例

代码示例来源:origin: com.j256.ormlite/ormlite-core

  1. /**
  2. * @see Dao#setObjectCache(boolean)
  3. */
  4. @Override
  5. public void setObjectCache(boolean enabled) {
  6. try {
  7. dao.setObjectCache(enabled);
  8. } catch (SQLException e) {
  9. logMessage(e, "setObjectCache(" + enabled + ") threw exception");
  10. throw new RuntimeException(e);
  11. }
  12. }

代码示例来源:origin: j256/ormlite-core

  1. /**
  2. * @see Dao#setObjectCache(boolean)
  3. */
  4. @Override
  5. public void setObjectCache(boolean enabled) {
  6. try {
  7. dao.setObjectCache(enabled);
  8. } catch (SQLException e) {
  9. logMessage(e, "setObjectCache(" + enabled + ") threw exception");
  10. throw new RuntimeException(e);
  11. }
  12. }

代码示例来源:origin: j256/ormlite-core

  1. /**
  2. * @see Dao#setObjectCache(ObjectCache)
  3. */
  4. @Override
  5. public void setObjectCache(ObjectCache objectCache) {
  6. try {
  7. dao.setObjectCache(objectCache);
  8. } catch (SQLException e) {
  9. logMessage(e, "setObjectCache threw exception on " + objectCache);
  10. throw new RuntimeException(e);
  11. }
  12. }

代码示例来源:origin: com.j256.ormlite/ormlite-core

  1. /**
  2. * @see Dao#setObjectCache(ObjectCache)
  3. */
  4. @Override
  5. public void setObjectCache(ObjectCache objectCache) {
  6. try {
  7. dao.setObjectCache(objectCache);
  8. } catch (SQLException e) {
  9. logMessage(e, "setObjectCache threw exception on " + objectCache);
  10. throw new RuntimeException(e);
  11. }
  12. }

代码示例来源:origin: j256/ormlite-core

  1. @Override
  2. protected ObjectCache enableCache(Dao<?, ?> dao) throws Exception {
  3. ReferenceObjectCache cache = ReferenceObjectCache.makeWeakCache();
  4. dao.setObjectCache(cache);
  5. return cache;
  6. }
  7. }

代码示例来源:origin: j256/ormlite-core

  1. @Override
  2. protected ObjectCache enableCache(Dao<?, ?> dao) throws Exception {
  3. LruObjectCache cache = new LruObjectCache(10);
  4. dao.setObjectCache(cache);
  5. return cache;
  6. }
  7. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testSetObjectCache() throws Exception {
  3. @SuppressWarnings("unchecked")
  4. Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  5. RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  6. dao.setObjectCache(false);
  7. replay(dao);
  8. rtDao.setObjectCache(false);
  9. verify(dao);
  10. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testSetObjectCacheCache() throws Exception {
  3. @SuppressWarnings("unchecked")
  4. Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  5. RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  6. dao.setObjectCache(null);
  7. replay(dao);
  8. rtDao.setObjectCache(null);
  9. verify(dao);
  10. }

代码示例来源:origin: j256/ormlite-core

  1. @Test(expected = RuntimeException.class)
  2. public void testSetObjectCacheThrow() throws Exception {
  3. @SuppressWarnings("unchecked")
  4. Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  5. RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  6. dao.setObjectCache(false);
  7. expectLastCall().andThrow(new SQLException("Testing catch"));
  8. replay(dao);
  9. rtDao.setObjectCache(false);
  10. verify(dao);
  11. }

代码示例来源:origin: j256/ormlite-core

  1. @Test(expected = RuntimeException.class)
  2. public void testSetObjectCacheCacheThrow() throws Exception {
  3. @SuppressWarnings("unchecked")
  4. Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  5. RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  6. dao.setObjectCache(null);
  7. expectLastCall().andThrow(new SQLException("Testing catch"));
  8. replay(dao);
  9. rtDao.setObjectCache(null);
  10. verify(dao);
  11. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testEnableBoolean() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. dao.setObjectCache(true);
  5. Foo foo = new Foo();
  6. int val = 12312321;
  7. foo.val = val;
  8. assertEquals(1, dao.create(foo));
  9. Foo result = dao.queryForId(foo.id);
  10. assertSame(foo, result);
  11. dao.setObjectCache(false);
  12. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testQueryAll() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. dao.setObjectCache(true);
  5. Foo foo = new Foo();
  6. int val = 12312321;
  7. foo.val = val;
  8. assertEquals(1, dao.create(foo));
  9. dao.clearObjectCache();
  10. List<Foo> results = dao.queryForAll();
  11. assertEquals(1, results.size());
  12. assertEquals(foo.id, results.get(0).id);
  13. assertNotSame(foo, results.get(0));
  14. Foo foo2 = dao.queryForId(foo.id);
  15. assertNotNull(foo2);
  16. assertEquals(foo.id, results.get(0).id);
  17. assertSame(results.get(0), foo2);
  18. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testDisableAlreadyDisabled() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. dao.setObjectCache(false);
  5. Foo foo = new Foo();
  6. int val = 12312321;
  7. foo.val = val;
  8. assertEquals(1, dao.create(foo));
  9. Foo result = dao.queryForId(foo.id);
  10. assertNotSame(foo, result);
  11. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testReplaceCache() throws Exception {
  3. Dao<Foo, Object> dao = createDao(Foo.class, true);
  4. ReferenceObjectCache cache1 = new ReferenceObjectCache(true);
  5. dao.setObjectCache(cache1);
  6. Foo foo = new Foo();
  7. int val = 12312321;
  8. foo.val = val;
  9. assertEquals(1, dao.create(foo));
  10. Foo result = dao.queryForId(foo.id);
  11. assertSame(foo, result);
  12. // enable a new cache
  13. dao.setObjectCache(new ReferenceObjectCache(true));
  14. assertEquals(0, cache1.size(Foo.class));
  15. result = dao.queryForId(foo.id);
  16. assertNotSame(foo, result);
  17. }

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

  1. @Test
  2. public void testBasicCacheStuff() throws Exception {
  3. Dao<AllTypes, Integer> allTypesDao = createDao(AllTypes.class, true);
  4. allTypesDao.setObjectCache(true);
  5. Dao<ForeignWrapper, Integer> wrapperDao = createDao(ForeignWrapper.class, true);
  6. wrapperDao.setObjectCache(true);
  7. AllTypes allTypes1 = new AllTypes();
  8. assertEquals(1, allTypesDao.create(allTypes1));
  9. ForeignWrapper wrapper = new ForeignWrapper();
  10. wrapper.foreign = allTypes1;
  11. assertEquals(1, wrapperDao.create(wrapper));
  12. ForeignWrapper wrapperResult = wrapperDao.queryForId(wrapper.id);
  13. assertSame(wrapper, wrapperResult);
  14. AllTypes allTypesResult = allTypesDao.queryForId(allTypes1.id);
  15. assertSame(allTypes1, allTypesResult);
  16. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testBasic() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. enableCache(dao);
  5. Foo foo = new Foo();
  6. int val = 12312321;
  7. foo.val = val;
  8. assertEquals(1, dao.create(foo));
  9. Foo result = dao.queryForId(foo.id);
  10. assertSame(foo, result);
  11. List<Foo> results = dao.queryForAll();
  12. assertEquals(1, results.size());
  13. assertSame(result, results.get(0));
  14. // disable cache
  15. dao.setObjectCache(null);
  16. result = dao.queryForId(foo.id);
  17. assertNotSame(foo, result);
  18. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testQueryForId() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. Foo foo = new Foo();
  5. int val = 12312321;
  6. foo.val = val;
  7. assertEquals(1, dao.create(foo));
  8. LruObjectCache cache = new LruObjectCache(1024);
  9. dao.setObjectCache(cache);
  10. Foo foo2 = dao.queryForId(foo.id);
  11. assertNotSame(foo, foo2);
  12. Foo foo3 = dao.queryForId(foo.id);
  13. assertSame(foo2, foo3);
  14. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testForeignLinkageWithCache() throws Exception {
  3. Dao<EagerConnection, Integer> multipleDao = createDao(EagerConnection.class, true);
  4. multipleDao.setObjectCache(true);
  5. Dao<EagerNode, Integer> foreignDao = createDao(EagerNode.class, true);
  6. foreignDao.setObjectCache(true);
  7. EagerNode foreign1 = new EagerNode();
  8. assertEquals(1, foreignDao.create(foreign1));
  9. EagerNode foreign2 = new EagerNode();
  10. assertEquals(1, foreignDao.create(foreign2));
  11. EagerConnection multiple1 = new EagerConnection();
  12. multiple1.from = foreign1;
  13. multiple1.to = foreign2;
  14. assertEquals(1, multipleDao.create(multiple1));
  15. EagerNode result = foreignDao.queryForId(foreign1.id);
  16. assertNotNull(result.froms);
  17. EagerConnection[] array = result.froms.toArray(new EagerConnection[result.froms.size()]);
  18. assertEquals(1, array.length);
  19. assertSame(foreign1, array[0].from);
  20. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testForeignInCache() throws Exception {
  3. Dao<ForeignParent, Integer> parentDao = createDao(ForeignParent.class, true);
  4. Dao<ForeignForeign, Integer> foreignDao = createDao(ForeignForeign.class, true);
  5. foreignDao.setObjectCache(true);
  6. ForeignForeign foreign = new ForeignForeign();
  7. foreign.stuff = "hello";
  8. foreignDao.create(foreign);
  9. assertSame(foreign, foreignDao.queryForId(foreign.id));
  10. ForeignParent parent = new ForeignParent();
  11. parent.foreign = foreign;
  12. parentDao.create(parent);
  13. ForeignParent result = parentDao.queryForId(parent.id);
  14. assertNotSame(parent, result);
  15. assertSame(foreign, result.foreign);
  16. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testClear() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. LruObjectCache cache = new LruObjectCache(2);
  5. dao.setObjectCache(cache);
  6. Foo foo = new Foo();
  7. int val = 12312321;
  8. foo.val = val;
  9. assertEquals(1, dao.create(foo));
  10. assertEquals(1, cache.size(Foo.class));
  11. Foo result = dao.queryForId(foo.id);
  12. assertSame(foo, result);
  13. dao.clearObjectCache();
  14. result = dao.queryForId(foo.id);
  15. assertNotSame(foo, result);
  16. }

相关文章