Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 700

db4o-doc-8.0-1.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Configuration|File Configuration|Storage|CachingStorage" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Cache Configuration Example	</title>
        <script type="text/javascript">/* <![CDATA[ */
window.onload = function(){
	var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf';
	ZeroClipboard.setMoviePath(pathToFlash);
			
	function bindToClipBord(element,content){
		var clip = new ZeroClipboard.Client();
		clip.setText(content);
		clip.glue(element);
	};
		
	if(location.protocol==='file:'){
		$('.copylink-marker').remove();
	} else{
			$('.copylink-marker').each(function(){
				var text = $(this).parent().parent().children('.prettyprint').html();
				$(this).hover(function(){
					bindToClipBord(this,text);
				},
				function(){});
			});	
	}		
	prettyPrint();	
};
                /* ]]> */</script>
        <link href="../../../../SkinSupport/MadCap.css" rel="stylesheet" />
        <link href="../../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" />
        <script src="../../../../SkinSupport/MadCapAll.js">
        </script>
        <script src="../../../../Resources/Code/prettify.js">
        </script>
        <script src="../../../../Resources/Code/lang-vb.js">
        </script>
        <script src="../../../../Resources/Code/jquery.min.js">
        </script>
        <script src="../../../../Resources/Code/ZeroClipboard.js">
        </script>
    </head>
    <body>
        <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../../index_CSH.html#configuration/file/storage/cachingstorage/cache_configuration_example.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../../configuration.htm">Configuration</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../../../file_configuration.htm">File Configuration</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../../storage.htm">Storage</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Cache Configuration Example</span>
        </div>
        <p>
            <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">');
				document.write("Direct Link");
			document.write('</a>');/*]]>*/</script>
        </p>
        <p>
        </p>
        <h1>Cache Configuration Example</h1>
        <p>The following example helps to see the effect of cache
by modifying the cache size. Note, that this example is not a good illustration
of the LRU cache algorithm, and only shows the effect of simple caching of the
most recent data.</p>
        <p>For the test we will use the following 2 classes:
Item and ItemStore, holding a collection of items:</p>
        <p MadCap:conditions="Global.Primary:java" />
        <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages">
            <pre class="prettyprint" xml:space="preserve">CachingStorageExample.cs: Item
private class Item {
    int id;
    String name;
    
    public Item(int id, String name) {
      this.id = id;
      this.name = name;
    }
    

    public override string ToString() {
      return String.Format("{0} [{1}]", name, id);
    }
  }</pre>
            <pre class="prettyprint" xml:space="preserve">CachingStorageExample.cs: ItemStore
private class ItemStore {
    List&lt;Item&gt; items;
    
    public ItemStore() {
      this.items = new List&lt;Item&gt;();
    }
    
    public void AddItem(Item item) {
      this.items.Add(item);
    }
  }</pre>
        </p>
        <p MadCap:conditions="Global.Primary:cs" />
        <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages">
            <pre class="prettyprint lang-vb" xml:space="preserve">CachingStorageExample.vb: Item
Private Class Item
            Private id As Integer
            Private name As [String]

            Public Sub  New(ByVal id As Integer, ByVal name As [String])
                Me.id = id
                Me.name = name
            End Sub


            Public Overloads Overrides Function ToString() As String
                Return [String].Format("{0} [{1}]", name, id)
            End Function
        End Class</pre>
            <pre class="prettyprint lang-vb" xml:space="preserve">CachingStorageExample.vb: ItemStore
Private Class ItemStore
            Private items As List(Of Item)

            Public Sub New()
                Me.items = New List(Of Item)()
            End Sub

            Public Sub AddItem(ByVal item As Item)
                Me.items.Add(item)
            End Sub
        End Class</pre>
        </p>
        <p MadCap:conditions="Global.Primary:vb" />
        <p>The following methods will be used to fill in and
query the database:</p>
        <p MadCap:conditions="Global.Primary:java" />
        <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages">
            <pre class="prettyprint" xml:space="preserve">CachingStorageExample.cs: CreateDatabase
private static void CreateDatabase(IEmbeddedConfiguration config)  {
    File.Delete(Db4oFileName);

      long startTime = DateTime.Now.Ticks;
    IObjectContainer container = Db4oEmbedded.OpenFile(config, Db4oFileName);
    try  {
      ItemStore itemStore = new ItemStore();
      for (int i = 0; i &lt; ObjectCount; i++)  {
        itemStore.AddItem(new Item(i, "title" + i));
      }
      container.Store(itemStore);
      Item item  = (Item) container.QueryByExample(
          new Item(1, "title1"))[0];
      System.Console.WriteLine(item);
    } finally  {
      container.Close();
    }
    System.Console.WriteLine(String.Format("Time to create a database: {0} ms",
            (DateTime.Now.Ticks - startTime)/TimeSpan.TicksPerMillisecond));
  }</pre>
            <pre class="prettyprint" xml:space="preserve">CachingStorageExample.cs: QueryDatabase
private static void QueryDatabase(IEmbeddedConfiguration config)  {

    IObjectContainer container = Db4oEmbedded
        .OpenFile(config, Db4oFileName);
    try  {
      List&lt;Item&gt; temp = new List&lt;Item&gt;();
        long startTime = DateTime.Now.Ticks;
      IQuery q = container.Query();
      q.Constrain(typeof(Item));
      q.Descend("id").Constrain(1).Greater();
      IObjectSet result = q.Execute();
      foreach (Item i in result) {
        temp.Add(i);
      }
      System.Console.WriteLine(String.Format("Time to get an objects first time: {0} ms",
          (DateTime.Now.Ticks - startTime)/TimeSpan.TicksPerMillisecond));
      //
      temp = new List&lt;Item&gt;();
        startTime = DateTime.Now.Ticks;
      foreach (Item i in result) {
        temp.Add(i);
      }
      System.Console.WriteLine(String.Format("Time to get an objects second time: {0} ms",
          (DateTime.Now.Ticks - startTime)/TimeSpan.TicksPerMillisecond));
    } finally  {
      container.Close();
    }
  }</pre>
        </p>
        <p MadCap:conditions="Global.Primary:cs" />
        <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages">
            <pre class="prettyprint lang-vb" xml:space="preserve">CachingStorageExample.vb: CreateDatabase
Private Shared Sub CreateDatabase(ByVal config As IEmbeddedConfiguration)
            File.Delete(Db4oFileName)

            Dim startTime As Long = DateTime.Now.Ticks
            Dim container As IObjectContainer = _ 
Db4oEmbedded.OpenFile(config, Db4oFileName)
            Try
                Dim itemStore As New ItemStore()
                For i As Integer = 0 To ObjectCount - 1
                    itemStore.AddItem(New Item(i, "title" &amp; i))
                Next
                container.Store(itemStore)
                Dim item As Item = _ 
DirectCast(container.QueryByExample(New Item(1, "title1"))(0), Item)
                System.Console.WriteLine(item)
            Finally
                container.Close()
            End Try
            System.Console.WriteLine([String].Format("Time to create a database: {0} ms", _ 
(DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond))
        End Sub</pre>
            <pre class="prettyprint lang-vb" xml:space="preserve">CachingStorageExample.vb: QueryDatabase
Private Shared Sub QueryDatabase(ByVal config As IEmbeddedConfiguration)

            Dim container As IObjectContainer = _ 
Db4oEmbedded.OpenFile(config, Db4oFileName)
            Try
                Dim temp As New List(Of Item)()
                Dim startTime As Long = DateTime.Now.Ticks
                Dim q As IQuery = container.Query()
                q.Constrain(GetType(Item))
                q.Descend("id").Constrain(1).Greater()
                Dim result As IObjectSet = q.Execute()
                For Each i As Item In result
                    temp.Add(i)
                Next
                System.Console.WriteLine([String].Format( _ 
"Time to get an objects first time: {0} ms", _
(DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond))
                '
                temp = New List(Of Item)()
                startTime = DateTime.Now.Ticks
                For Each i As Item In result
                    temp.Add(i)
                Next
                System.Console.WriteLine([String].Format(_ 
"Time to get an objects second time: {0} ms", _ 
(DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond))
            Finally
                container.Close()
            End Try
        End Sub</pre>
        </p>
        <p MadCap:conditions="Global.Primary:vb" />
        <p>In general you should see the benefits of using
cache with the majority of db4o operations. However, the effect is most obvious
when a large amount of database information should be accessed fast (query) and
this information is not loaded in the applications hash memory. We are trying
to reproduce this situation in the second method querying database. If the
amount of items in the ItemStore collection is 10000, the database size will be
around 900 KB. With the default cache size, i.e. page count multiplied by page
size (64 * 1024), this will mean that the whole collection won't fit into the
cache (It can still fit into your hash memory, so you may want to decrease the
available hash memory or increase the size of the collection to see the effect.).
</p>
        <p>We will use the following configuration methods to
compare the default cache allocation and the custom one:</p>
        <p MadCap:conditions="Global.Primary:java" />
        <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages">
            <pre class="prettyprint" xml:space="preserve">CachingStorageExample.cs: GetConfig
private static IEmbeddedConfiguration GetConfig()  {
    IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
    config.File.<span class="MCPopup"><a href="javascript:void(0);" class="MCPopupSpot" onclick="FMCPopup( event, this ); return false;" MadCap:src="../../storage.htm">Storage<img style="border: none;margin-left: 5px;" src="../../../../SkinSupport/ExpandingClosed.gif" MadCap:altsrc="../../../../SkinSupport/ExpandingOpen.gif" class="MCExpandingIcon" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../../../SkinSupport/ExpandingOpen.gif' ); }" /></a></span> = new CachingStorage(new FileStorage(), PageCount, PageSize);
    return config;
  }</pre>
            <pre class="prettyprint" xml:space="preserve">CachingStorageExample.cs: GetDefaultConfig
private static IEmbeddedConfiguration GetDefaultConfig()  {
    IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
    return config;
  }</pre>
        </p>
        <p MadCap:conditions="Global.Primary:cs" />
        <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages">
            <pre class="prettyprint lang-vb" xml:space="preserve">CachingStorageExample.vb: GetConfig
Private Shared Function GetConfig() As IEmbeddedConfiguration
            Dim config As IEmbeddedConfiguration = _ 
Db4oEmbedded.NewConfiguration()
            config.File.Storage = New _ 
CachingStorage(New FileStorage(), PageCount, PageSize)
            Return config
        End Function</pre>
            <pre class="prettyprint lang-vb" xml:space="preserve">CachingStorageExample.vb: GetDefaultConfig
Private Shared Function  GetDefaultConfig() As IEmbeddedConfiguration
            Dim config As IEmbeddedConfiguration = _ 
Db4oEmbedded.NewConfiguration()
            Return config
        End Function</pre>
        </p>
        <p MadCap:conditions="Global.Primary:vb" />
        <p>Using custom page count = 1024 will make the cache
size enough to fit the whole collection, and you should see some performance
improvement browsing the retrieved collection second time (Unless it all fits
into your hash memory in any case). Please, also try decreasing page size and
count to see the opposite effect.</p>
        <p MadCap:conditions="Primary.Online">Download example code:</p>
        <p MadCap:conditions="Primary.Online">
            <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"><a href="cachingstoragevb.zip">VB.NET </a>
            </MadCap:conditionalText>
            <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="cachingstoragecs.zip">c# </a>
            </MadCap:conditionalText>
        </p>
        <script type="text/javascript" src="../../../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>