📄 caches.sgml
字号:
<refentry id="glib-Caches"><refmeta><refentrytitle>Caches</refentrytitle><manvolnum>3</manvolnum><refmiscinfo>GLIB Library</refmiscinfo></refmeta><refnamediv><refname>Caches</refname><refpurpose>caches allow sharing of complex data structures to save resources.</refpurpose></refnamediv><refsynopsisdiv><title>Synopsis</title><synopsis>#include <glib.h>struct <link linkend="GCache">GCache</link>;<link linkend="GCache">GCache</link>* <link linkend="g-cache-new">g_cache_new</link> (<link linkend="GCacheNewFunc">GCacheNewFunc</link> value_new_func, <link linkend="GCacheDestroyFunc">GCacheDestroyFunc</link> value_destroy_func, <link linkend="GCacheDupFunc">GCacheDupFunc</link> key_dup_func, <link linkend="GCacheDestroyFunc">GCacheDestroyFunc</link> key_destroy_func, <link linkend="GHashFunc">GHashFunc</link> hash_key_func, <link linkend="GHashFunc">GHashFunc</link> hash_value_func, <link linkend="GEqualFunc">GEqualFunc</link> key_equal_func);<link linkend="gpointer">gpointer</link> <link linkend="g-cache-insert">g_cache_insert</link> (<link linkend="GCache">GCache</link> *cache, <link linkend="gpointer">gpointer</link> key);void <link linkend="g-cache-remove">g_cache_remove</link> (<link linkend="GCache">GCache</link> *cache, <link linkend="gconstpointer">gconstpointer</link> value);void <link linkend="g-cache-destroy">g_cache_destroy</link> (<link linkend="GCache">GCache</link> *cache);void <link linkend="g-cache-key-foreach">g_cache_key_foreach</link> (<link linkend="GCache">GCache</link> *cache, <link linkend="GHFunc">GHFunc</link> func, <link linkend="gpointer">gpointer</link> user_data);void <link linkend="g-cache-value-foreach">g_cache_value_foreach</link> (<link linkend="GCache">GCache</link> *cache, <link linkend="GHFunc">GHFunc</link> func, <link linkend="gpointer">gpointer</link> user_data);void (<link linkend="GCacheDestroyFunc">*GCacheDestroyFunc</link>) (<link linkend="gpointer">gpointer</link> value);<link linkend="gpointer">gpointer</link> (<link linkend="GCacheDupFunc">*GCacheDupFunc</link>) (<link linkend="gpointer">gpointer</link> value);<link linkend="gpointer">gpointer</link> (<link linkend="GCacheNewFunc">*GCacheNewFunc</link>) (<link linkend="gpointer">gpointer</link> key);</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>A <link linkend="GCache">GCache</link> allows sharing of complex data structures, in order to savesystem resources.</para><para>GTK+ uses caches for <link linkend="GtkStyles">GtkStyles</link> and <link linkend="GdkGCs">GdkGCs</link>. These consume a lot ofresources, so a <link linkend="GCache">GCache</link> is used to see if a <link linkend="GtkStyle">GtkStyle</link> or <link linkend="GdkGC">GdkGC</link> with therequired properties already exists. If it does, then the existingobject is used instead of creating a new one.</para><para><link linkend="GCache">GCache</link> uses keys and values.A <link linkend="GCache">GCache</link> key describes the properties of a particular resource.A <link linkend="GCache">GCache</link> value is the actual resource.</para></refsect1><refsect1><title>Details</title><refsect2><title><anchor id="GCache">struct GCache</title><programlisting>struct GCache;</programlisting><para>The <link linkend="GCache">GCache</link> struct is an opaque data structure containing information abouta <link linkend="GCache">GCache</link>. It should only be accessed via the following functions.</para></refsect2><refsect2><title><anchor id="g-cache-new">g_cache_new ()</title><programlisting><link linkend="GCache">GCache</link>* g_cache_new (<link linkend="GCacheNewFunc">GCacheNewFunc</link> value_new_func, <link linkend="GCacheDestroyFunc">GCacheDestroyFunc</link> value_destroy_func, <link linkend="GCacheDupFunc">GCacheDupFunc</link> key_dup_func, <link linkend="GCacheDestroyFunc">GCacheDestroyFunc</link> key_destroy_func, <link linkend="GHashFunc">GHashFunc</link> hash_key_func, <link linkend="GHashFunc">GHashFunc</link> hash_value_func, <link linkend="GEqualFunc">GEqualFunc</link> key_equal_func);</programlisting><para>Creates a new <link linkend="GCache">GCache</link>.</para><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>value_new_func</parameter> :</entry><entry>a function to create a new object given a key.This is called by <link linkend="g-cache-insert">g_cache_insert</link>() if an object with the given keydoes not already exist.</entry></row><row><entry align="right"><parameter>value_destroy_func</parameter> :</entry><entry>a function to destroy an object. It iscalled by <link linkend="g-cache-remove">g_cache_remove</link>() when the object is no longer needed (i.e. itsreference count drops to 0).</entry></row><row><entry align="right"><parameter>key_dup_func</parameter> :</entry><entry>a function to copy a key. It is called by<link linkend="g-cache-insert">g_cache_insert</link>() if the key does not already exist in the <link linkend="GCache">GCache</link>.</entry></row><row><entry align="right"><parameter>key_destroy_func</parameter> :</entry><entry>a function to destroy a key. It iscalled by <link linkend="g-cache-remove">g_cache_remove</link>() when the object is no longer needed (i.e. itsreference count drops to 0).</entry></row><row><entry align="right"><parameter>hash_key_func</parameter> :</entry><entry>a function to create a hash value from a key.</entry></row><row><entry align="right"><parameter>hash_value_func</parameter> :</entry><entry>a function to create a hash value from a value.</entry></row><row><entry align="right"><parameter>key_equal_func</parameter> :</entry><entry>a function to compare two keys. It should return <literal>TRUE</literal> ifthe two keys are equivalent.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>a new <link linkend="GCache">GCache</link>.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2><title><anchor id="g-cache-insert">g_cache_insert ()</title><programlisting><link linkend="gpointer">gpointer</link> g_cache_insert (<link linkend="GCache">GCache</link> *cache, <link linkend="gpointer">gpointer</link> key);</programlisting><para>Gets the value corresponding to the given key, creating it if necessary.It first checks if the value already exists in the <link linkend="GCache">GCache</link>, by usingthe <parameter>key_equal_func</parameter> function passed to <link linkend="g-cache-new">g_cache_new</link>().If it does already exist it is returned, and its reference count is increasedby one.If the value does not currently exist, if is created by calling the<parameter>value_new_func</parameter>. The key is duplicated by calling<parameter>key_dup_func</parameter> and the duplicated key and value are insertedinto the <link linkend="GCache">GCache</link>.</para><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>cache</parameter> :</entry><entry>a <link linkend="GCache">GCache</link>.</entry></row><row><entry align="right"><parameter>key</parameter> :</entry><entry>a key describing a <link linkend="GCache">GCache</link> object.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry>a pointer to a <link linkend="GCache">GCache</link> value.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -