You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlcontainer-caching.asciidoc 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ---
  2. title: Caching, Paging and Refreshing
  3. order: 5
  4. layout: page
  5. ---
  6. [[sqlcontainer.caching]]
  7. = Caching, Paging and Refreshing
  8. To decrease the amount of queries made to the database, SQLContainer uses
  9. internal caching for database contents. The caching is implemented with a
  10. size-limited [classname]#LinkedHashMap# containing a mapping from
  11. [classname]#RowId#s to [classname]#RowItem#s. Typically developers do not need
  12. to modify caching options, although some fine-tuning can be done if required.
  13. [[sqlcontainer.caching.container-size]]
  14. == Container Size
  15. The [classname]#SQLContainer# keeps continuously checking the amount of rows in
  16. the connected database table in order to detect external addition or removal of
  17. rows. By default, the table row count is assumed to remain valid for 10 seconds.
  18. This value can be altered from code; with
  19. [methodname]#setSizeValidMilliSeconds()# in [classname]#SQLContainer#.
  20. If the size validity time has expired, the row count will be automatically
  21. updated on:
  22. * A call to [methodname]#getItemIds()# method
  23. * A call to [methodname]#size()# method
  24. * Some calls to [methodname]#indexOfId(Object itemId)# method
  25. * A call to [methodname]#firstItemId()# method
  26. * When the container is fetching a set of rows to the item cache (lazy loading)
  27. [[sqlcontainer.caching.page-length]]
  28. == Page Length and Cache Size
  29. The page length of the [classname]#SQLContainer# dictates the amount of rows
  30. fetched from the database in one query. The default value is 100, and it can be
  31. modified with the [methodname]#setPageLength()# method. To avoid constant
  32. queries it is recommended to set the page length value to at least 5 times the
  33. amount of rows displayed in a Vaadin [classname]#Table#; obviously, this is also
  34. dependent on the cache ratio set for the [classname]#Table# component.
  35. The size of the internal item cache of the [classname]#SQLContainer# is
  36. calculated by multiplying the page length with the cache ratio set for the
  37. container. The cache ratio can only be set from the code, and the default value
  38. for it is 2. Hence with the default page length of 100 the internal cache size
  39. becomes 200 items. This should be enough even for larger [classname]#Table#s
  40. while ensuring that no huge amounts of memory will be used on the cache.
  41. [[sqlcontainer.caching.refreshing]]
  42. == Refreshing the Container
  43. Normally, the [classname]#SQLContainer# will handle refreshing automatically
  44. when required. However, there may be situations where an implicit refresh is
  45. needed, for example, to make sure that the version column is up-to-date prior to
  46. opening the item for editing in a form. For this purpose a
  47. [methodname]#refresh()# method is provided. This method simply clears all
  48. caches, resets the current item fetching offset and sets the container size
  49. dirty. Any item-related call after this will inevitably result into row count
  50. and item cache update.
  51. __Note that a call to the refresh method will not affect or reset the following
  52. properties of the container:__
  53. * The [classname]#QueryDelegate# of the container
  54. * Auto-commit mode
  55. * Page length
  56. * Filters
  57. * Sorting
  58. ifdef::web[]
  59. [[sqlcontainer.caching.flush-notification]]
  60. == Cache Flush Notification Mechanism
  61. Cache usage with databases in multiuser applications always results in some kind
  62. of a compromise between the amount of queries we want to execute on the database
  63. and the amount of memory we want to use on caching the data; and most
  64. importantly, risking the cached data becoming stale.
  65. SQLContainer provides an experimental remedy to this problem by implementing a
  66. simple cache flush notification mechanism. Due to its nature these notifications
  67. are disabled by default but can be easily enabled for a container instance by
  68. calling [methodname]#enableCacheFlushNotifications()# at any time during the
  69. lifetime of the container.
  70. The notification mechanism functions by storing a weak reference to all
  71. registered containers in a static list structure. To minimize the risk of memory
  72. leaks and to avoid unlimited growing of the reference list, dead weak references
  73. are collected to a reference queue and removed from the list every time a
  74. [classname]#SQLContainer# is added to the notification reference list or a
  75. container calls the notification method.
  76. When a [classname]#SQLContainer# has its cache notifications set enabled, it
  77. will call the static [methodname]#notifyOfCacheFlush()# method giving itself as
  78. a parameter. This method will compare the notifier-container to all the others
  79. present in the reference list. To fire a cache flush event, the target container
  80. must have the same type of [classname]#QueryDelegate# (either
  81. [classname]#TableQuery# or [classname]#FreeformQuery#) and the table name or
  82. query string must match with the container that fired the notification. If a
  83. match is found the [methodname]#refresh()# method of the matching container is
  84. called, resulting in cache flushing in the target container.
  85. __Note: Standard Vaadin issues apply; even if the [classname]#SQLContainer# is
  86. refreshed on the server side, the changes will not be reflected to the UI until
  87. a server round-trip is performed, or unless a push mechanism is used.__
  88. endif::web[]