﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Beyond Anything!-文章分类-wp7</title><link>http://www.cppblog.com/msopengl/category/16013.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 16 Jul 2011 04:36:57 GMT</lastBuildDate><pubDate>Sat, 16 Jul 2011 04:36:57 GMT</pubDate><ttl>60</ttl><item><title>Caching</title><link>http://www.cppblog.com/msopengl/articles/148427.html</link><dc:creator>Skill</dc:creator><author>Skill</author><pubDate>Fri, 10 Jun 2011 07:10:00 GMT</pubDate><guid>http://www.cppblog.com/msopengl/articles/148427.html</guid><wfw:comment>http://www.cppblog.com/msopengl/comments/148427.html</wfw:comment><comments>http://www.cppblog.com/msopengl/articles/148427.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/msopengl/comments/commentRss/148427.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/msopengl/services/trackbacks/148427.html</trackback:ping><description><![CDATA[
		<h1 class="title">
				<font size="2">引自：  http://msdn.microsoft.com/en-us/library/aa383928.aspx<br /></font>
		</h1>
		<h1 class="title">
				<font size="2">Caching</font>
		</h1>
		<div class="clsServerSDKContent">
		</div>
		<p>The WinINet functions have simple, yet flexible, built-in caching 
support. Any data retrieved from the network is cached on the hard disk 
and retrieved for subsequent requests. The application can control the 
caching on each request. For 
http requests from the server, most headers received are also cached. 
When an 
http request is satisfied from the cache, the cached headers are also 
returned to the caller. This makes data download seamless, whether the 
data is coming from the cache or from the network. </p>
		<p>Applications must properly allocate a buffer in order to get the 
desired results when using the persistent URL caching functions. For 
more information, see 
<a href="http://msdn.microsoft.com/en-us/library/aa383869.aspx">Using Buffers</a>. </p>
		<h3>Cache Behavior During Response Processing</h3>
		<p>The WinINet cache is  compliant with the HTTP cache-control 
directives described in RFC 2616. The cache-control directives and 
application set flags determine what may be cached; however, WinINet 
determines  what is actually cached based on the following criterion:</p>
		<p>
		</p>
		<ul>
				<li>WinINet only caches HTTP and FTP responses.</li>
				<li>Only well behaved responses may be stored by a cache and used in a 
reply to a subsequent Request. Well behaved responses are defined as 
responses that return successfully.</li>
				<li>By default, WinINet will cache successful responses unless either a 
cache-control directive from the server, or an application-defined flag 
specifically denote that the response may not be cached.</li>
				<li>In general, responses to the GET verb are cached if the requirements
 listed above are met. Responses to PUT and POST verbs are not cached 
under any circumstances.</li>
				<li>Items will be cached even when the cache is full. If an added item 
is puts the cache over the size limit, the cache scavenger is scheduled.
 By default, items are not guaranteed to remain more than 10 minutes in 
the cache. For more information, see the <a href="http://msdn.microsoft.com/en-us/library/aa383928.aspx#scavenger">Cache Scavenger</a> section below.</li>
				<li>Https is cached by default. This is managed by a global setting  
that cannot be overridden by application-defined cache directives. To 
override the global setting, select the Internet Options applet in the 
control panel, and go to the advanced tab. Check the "Do not save  
encrypted pages to disk" box under the "Security" section.</li>
		</ul>
		<h3>Cache Scavenger</h3>
		<p>The cache scavenger periodically  cleans items from the cache. If an 
item is added to the cache and the cache is full, the item is added to 
the cache and the cache scavenger is scheduled. If the cache scavenger 
completes a round of scavenging and the cache has not reached the cache 
limit, the scavenger is scheduled for another round when another item is
 added to the cache. In general, the scavenger is scheduled when an 
added item puts the cache over its size limit. By default, the minimum 
time to live in the cache is set to 10 minutes unless otherwise 
specified in a cache-control directive. When the cache scavenger is 
initiated, there is no guarantee that the oldest items are the first to 
be deleted from the cache.</p>
		<p>The cache is shared across all WinINet applications on the computer 
for the same  user. Starting with  Windows Vista and Windows Server 2008
 the cache size is set to 1/32nd the size of the disk, with a  minimum 
size of  8MB and a maximum size of 50MB.</p>
		<h3>Using Flags to Control Caching
    
   </h3>
		<p>The caching flags allow an application to control when and how it 
uses the cache. These flags can be used alone or in combination with the
 <em>dwFlags</em> parameter in functions that access information or 
resources on the Internet. By default, the functions store all data 
downloaded from the Internet.</p>
		<p>The following flags can be used to control caching.</p>
		<table class="clsStd">
				<tbody>
						<tr>
								<th>Value</th>
								<th>Meaning</th>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_cache_async">INTERNET_FLAG_CACHE_ASYNC</a>
								</td>
								<td>This flag has no effect.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_cache_if_net_fail">INTERNET_FLAG_CACHE_IF_NET_FAIL</a>
								</td>
								<td>Returns the resource from the cache if the network request for the resource fails due to an 
<a href="http://msdn.microsoft.com/en-us/library/aa385465.aspx#error_internet_connection_reset">ERROR_INTERNET_CONNECTION_RESET</a>  or 
<a href="http://msdn.microsoft.com/en-us/library/aa385465.aspx#error_internet_cannot_connect">ERROR_INTERNET_CANNOT_CONNECT</a> error. This flag is used by 
<a href="http://msdn.microsoft.com/en-us/library/aa384233.aspx"><strong>HttpOpenRequest</strong></a>.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_dont_cache">INTERNET_FLAG_DONT_CACHE</a>
								</td>
								<td>Does not cache the data, either locally or in any gateways. Identical to the preferred value, 
<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#INTERNET_FLAG_NO_CACHE_WRITE">INTERNET_FLAG_NO_CACHE_WRITE</a>.</td>
						</tr>
						<tr>
								<td>
										<br />
								</td>
								<td>Indicates that this is a Forms submission.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_from_cache">INTERNET_FLAG_FROM_CACHE</a>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_forms_submit">INTERNET_FLAG_FORMS_SUBMIT</a>
								</td>
								<td>Does
 not make network requests. All entities are returned from the cache. If
 the requested item is not in the cache, a suitable error, such as 
ERROR_FILE_NOT_FOUND, is returned. Only the 
<a href="http://msdn.microsoft.com/en-us/library/aa385096.aspx"><strong>InternetOpen</strong></a> function uses this flag.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_fwd_back">INTERNET_FLAG_FWD_BACK</a>
								</td>
								<td>Indicates
 that the function should use the copy of the resource that is currently
 in the Internet cache. The expiration date and other information about 
the resource is not checked. If the requested item is not found in the 
Internet cache, the system  attempts to locate the resource on the 
network. This value was introduced in Microsoft Internet Explorer 5 and 
is associated with the <strong>Forward</strong> and <strong>Back</strong> button operations of Internet Explorer.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_hyperlink">INTERNET_FLAG_HYPERLINK</a>
								</td>
								<td>Forces
 the application to reload a resource if no expire time and no 
last-modified time were returned when the resource was stored in the 
cache.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_make_persistent">INTERNET_FLAG_MAKE_PERSISTENT</a>
								</td>
								<td>No longer supported.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_must_cache_request">INTERNET_FLAG_MUST_CACHE_REQUEST</a>
								</td>
								<td>Causes a temporary file to be created if the file cannot be cached. This is identical to the preferred value, 
<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#INTERNET_FLAG_NEED_FILE">INTERNET_FLAG_NEED_FILE</a>. </td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_need_file">INTERNET_FLAG_NEED_FILE</a>
								</td>
								<td>Causes a temporary file to be created if the file cannot be cached. </td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_no_cache_write">INTERNET_FLAG_NO_CACHE_WRITE</a>
								</td>
								<td>Rejects
 any attempt by the function to store data downloaded from the Internet 
in the cache. This flag is necessary if the application does not want 
any downloaded resources to be stored locally. </td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_no_ui">INTERNET_FLAG_NO_UI</a>
								</td>
								<td>Disables the cookie dialog box. This flag can be used by 
<a href="http://msdn.microsoft.com/en-us/library/aa384233.aspx"><strong>HttpOpenRequest</strong></a> and 
<a href="http://msdn.microsoft.com/en-us/library/aa385098.aspx"><strong>InternetOpenUrl</strong></a> (HTTP requests only).</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_offline">INTERNET_FLAG_OFFLINE</a>
								</td>
								<td>Prevents
 the application from sending requests to the network. All requests are 
resolved using the resources stored in the cache. If the resource is not
 in the cache, a suitable error, such as ERROR_FILE_NOT_FOUND, is 
returned.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_pragma_no_cache">INTERNET_FLAG_PRAGMA_NO_CACHE</a>
								</td>
								<td>Forces the request to be resolved by the origin server, even if a cached copy exists on the proxy. The 
<a href="http://msdn.microsoft.com/en-us/library/aa385098.aspx"><strong>InternetOpenUrl</strong></a> function (on HTTP and HTTPS requests only) and 
<a href="http://msdn.microsoft.com/en-us/library/aa384233.aspx"><strong>HttpOpenRequest</strong></a> function use this flag.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_reload">INTERNET_FLAG_RELOAD</a>
								</td>
								<td>Forces
 the function to retrieve the requested resource directly from the 
Internet. The information that is downloaded is stored in the cache.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#internet_flag_resynchronize">INTERNET_FLAG_RESYNCHRONIZE</a>
								</td>
								<td>Causes
 an application to perform a conditional download of the resource from 
the Internet. If the version stored in the cache is current, the 
information is downloaded from the cache. Otherwise, the information is 
reloaded from the server.</td>
						</tr>
				</tbody>
		</table>
		<p> </p>
		<h3>Persistent Caching Functions
    
   </h3>
		<p>Clients that need persistent caching services use the persistent 
caching functions to allow their applications to save data in the local 
file system for subsequent use, such as in situations where a 
low-bandwidth link limits access to the data, or the access is not 
available at all. </p>
		<p>The cache functions provide persistent caching and offline browsing. Unless the 
<a href="http://msdn.microsoft.com/en-us/library/aa383661.aspx#INTERNET_FLAG_NO_CACHE_WRITE">INTERNET_FLAG_NO_CACHE_WRITE</a>
 flag explicitly specifies no caching, the functions cache all data 
downloaded from the network. The responses to POST data are not cached. </p>
		<h3>Using the Persistent URL Cache Functions
    
   </h3>
		<p>The following persistent URL cache functions allow an application to access and manipulate information stored in the cache.</p>
		<table class="clsStd">
				<tbody>
						<tr>
								<th>Function</th>
								<th>Description</th>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/ff384269.aspx">
												<strong>CommitUrlCacheEntryA</strong>
										</a>
								</td>
								<td>Caches data in the specified file in the cache storage and associates it with the given URL.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/ff384270.aspx">
												<strong>CommitUrlCacheEntryW</strong>
										</a>
								</td>
								<td>Caches data in the specified file in the cache storage and associates it with the given URL.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383968.aspx">
												<strong>CreateUrlCacheEntry</strong>
										</a>
								</td>
								<td>Allocates
 the requested cache storage and creates a local file name for saving 
the cache entry that corresponds to the source name.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383972.aspx">
												<strong>CreateUrlCacheGroup</strong>
										</a>
								</td>
								<td>Generates a cache group identification.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383983.aspx">
												<strong>DeleteUrlCacheEntry</strong>
										</a>
								</td>
								<td>Removes the file associated with the source name from the cache, if the file exists.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa383990.aspx">
												<strong>DeleteUrlCacheGroup</strong>
										</a>
								</td>
								<td>Releases a <strong>GROUPID</strong> and any associated state in the cache index file.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa384015.aspx">
												<strong>FindCloseUrlCache</strong>
										</a>
								</td>
								<td>Closes the specified enumeration handle.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa384026.aspx">
												<strong>FindFirstUrlCacheEntry</strong>
										</a>
								</td>
								<td>Begins the enumeration of the cache.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa384034.aspx">
												<strong>FindFirstUrlCacheEntryEx</strong>
										</a>
								</td>
								<td>Begins a filtered enumeration of the cache.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa384049.aspx">
												<strong>FindNextUrlCacheEntry</strong>
										</a>
								</td>
								<td>Retrieves the next entry in the cache.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa384057.aspx">
												<strong>FindNextUrlCacheEntryEx</strong>
										</a>
								</td>
								<td>Retrieves the next entry in a filtered cache enumeration.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa384185.aspx">
												<strong>GetUrlCacheEntryInfo</strong>
										</a>
								</td>
								<td>Retrieves information about a cache entry.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa384188.aspx">
												<strong>GetUrlCacheEntryInfoEx</strong>
										</a>
								</td>
								<td>Searches for the URL after translating any cached redirections that would be applied in offline mode by 
<a href="http://msdn.microsoft.com/en-us/library/aa384247.aspx"><strong>HttpSendRequest</strong></a>.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa385354.aspx">
												<strong>ReadUrlCacheEntryStream</strong>
										</a>
								</td>
								<td>Reads the cached data from a stream that has been opened using 
<a href="http://msdn.microsoft.com/en-us/library/aa385368.aspx"><strong>RetrieveUrlCacheEntryStream</strong></a>.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa385365.aspx">
												<strong>RetrieveUrlCacheEntryFile</strong>
										</a>
								</td>
								<td>Retrieves a cache entry from the cache in the form of a file.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa385368.aspx">
												<strong>RetrieveUrlCacheEntryStream</strong>
										</a>
								</td>
								<td>Provides the most efficient and implementation-independent way of accessing the cache data.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa385390.aspx">
												<strong>SetUrlCacheEntryGroup</strong>
										</a>
								</td>
								<td>Adds or removes entries from a cache group.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa385396.aspx">
												<strong>SetUrlCacheEntryInfo</strong>
										</a>
								</td>
								<td>Sets the specified members of the 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a> structure.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa385407.aspx">
												<strong>UnlockUrlCacheEntryFile</strong>
										</a>
								</td>
								<td>Unlocks the cache entry that was locked when the file was retrieved for use from the cache by 
<a href="http://msdn.microsoft.com/en-us/library/aa385365.aspx"><strong>RetrieveUrlCacheEntryFile</strong></a>.</td>
						</tr>
						<tr>
								<td>
										<a href="http://msdn.microsoft.com/en-us/library/aa385415.aspx">
												<strong>UnlockUrlCacheEntryStream</strong>
										</a>
								</td>
								<td>Closes the stream that has been retrieved using 
<a href="http://msdn.microsoft.com/en-us/library/aa385368.aspx"><strong>RetrieveUrlCacheEntryStream</strong></a>.</td>
						</tr>
				</tbody>
		</table>
		<p> </p>
		<h4>Enumerating the Cache
      
     </h4>
		<p>The 
<a href="http://msdn.microsoft.com/en-us/library/aa384026.aspx"><strong>FindFirstUrlCacheEntry</strong></a> and 
<a href="http://msdn.microsoft.com/en-us/library/aa384049.aspx"><strong>FindNextUrlCacheEntry</strong></a> functions enumerate the information stored in the cache. 
<a href="http://msdn.microsoft.com/en-us/library/aa384026.aspx"><strong>FindFirstUrlCacheEntry</strong></a>
 starts the enumeration by taking a search pattern, a buffer, and a 
buffer size to create a handle and return the first cache entry. 
<a href="http://msdn.microsoft.com/en-us/library/aa384049.aspx"><strong>FindNextUrlCacheEntry</strong></a> takes the handle created by 
<a href="http://msdn.microsoft.com/en-us/library/aa384026.aspx"><strong>FindFirstUrlCacheEntry</strong></a>, a buffer, and a buffer size to return the next cache entry. </p>
		<p>Both functions store an 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a>
 structure in the buffer. The size of this structure varies for each 
entry. If the buffer size passed to either function is insufficient, the
 function fails and 
<a href="http://msdn.microsoft.com/en-us/library/ms679360.aspx"><strong>GetLastError</strong></a>
 returns ERROR_INSUFFICIENT_BUFFER. The buffer size variable contains 
the buffer size that was needed to retrieve that cache entry. A buffer 
of the size indicated by the buffer size variable should be allocated, 
and the function should be called again with the new buffer. </p>
		<p>The 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a>
 structure contains the structure size, URL of the cached information, 
local file name, cache entry type, use count, hit rate, size, last 
modified time, expiration, last access,  last synchronized time, header 
information, header information size, and file name extension. </p>
		<p>The 
<a href="http://msdn.microsoft.com/en-us/library/aa384026.aspx"><strong>FindFirstUrlCacheEntry</strong></a> function takes a search pattern, a buffer that stores the 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a> structure, and the buffer size. Currently, only the default search pattern, which returns all cache entries, is implemented. </p>
		<p>After the cache is enumerated, the application should call 
<a href="http://msdn.microsoft.com/en-us/library/aa384015.aspx"><strong>FindCloseUrlCache</strong></a> to close the cache enumeration handle. </p>
		<p>The following example displays each cache entry's URL in a list box, <strong>IDC_CacheList</strong>.
 It uses MAX_CACHE_ENTRY_INFO_SIZE to initially allocate a buffer, since
  early versions of the WinINet API do not enumerate the cache properly 
otherwise. Later versions do enumerate the cache properly and there is 
no cache size limit. All applications that run on computers with the 
version of the WinINet API from Internet Explorer 4.0 must allocate a 
buffer of the required size. For more information, see 
<a href="http://msdn.microsoft.com/en-us/library/aa383869.aspx">Using Buffers</a>. </p>
		<div class="LW_CodeSnippetContainer">
				<a name="CodeSpippet0">
				</a>
				<div class="LW_CodeSnippetContainerCodeCollection">
						<div class="LW_CodeSnippetToolBar">
								<div class="LW_CodeSnippetToolBarText" style="">
										<a name="CodeSnippetname" style="display: none;" title="Copy to clipboard.">Copy</a>
								</div>
						</div>
						<div id="CodeSnippetContainerCode0" class="LW_CodeSnippetContainerCode">
								<div style="color: Black;">
										<pre>int WINAPI EnumerateCacheOld(HWND hX)<br />{<br />    DWORD dwEntrySize;<br />    LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry;<br />    DWORD MAX_CACHE_ENTRY_INFO_SIZE = 4096;<br />    HANDLE hCacheDir;<br />    int nCount=0;<br /><br />    SendDlgItemMessage(hX,IDC_CacheList,LB_RESETCONTENT,0,0);<br /><br />    SetCursor(LoadCursor(NULL,IDC_WAIT));<br /><br />    dwEntrySize = MAX_CACHE_ENTRY_INFO_SIZE;<br />    lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize];<br />    lpCacheEntry-&gt;dwStructSize = dwEntrySize;<br /><br />again:<br /><br />    hCacheDir = FindFirstUrlCacheEntry(NULL,<br />                                       lpCacheEntry,<br />                                       &amp;dwEntrySize);<br />    if (!hCacheDir)                                             <br />    {<br />        delete[]lpCacheEntry;<br />        switch(GetLastError())<br />        {<br />            case ERROR_NO_MORE_ITEMS: <br />                TCHAR tempout[80];<br />                _stprintf_s(tempout, <br />                            80,   <br />                            TEXT("The number of cache entries = %d \n"),<br />                            nCount);<br />                MessageBox(hX,tempout,TEXT("Cache Enumeration"),MB_OK);<br />                FindCloseUrlCache(hCacheDir);<br />                SetCursor(LoadCursor(NULL,IDC_ARROW));<br />                return TRUE;<br />                break;<br />            case ERROR_INSUFFICIENT_BUFFER:<br />                lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) <br />                                new char[dwEntrySize];<br />                lpCacheEntry-&gt;dwStructSize = dwEntrySize;<br />                goto again;<br />                break;<br />            default:<br />                ErrorOut( hX,GetLastError(),<br />                          TEXT("FindNextUrlCacheEntry Init"));<br />                FindCloseUrlCache(hCacheDir);<br />                SetCursor(LoadCursor(NULL,IDC_ARROW));<br />                return FALSE;<br />        }<br />    }<br /><br />    SendDlgItemMessage(hX,IDC_CacheList,LB_ADDSTRING,<br />                       0,(LPARAM)(lpCacheEntry-&gt;lpszSourceUrlName));<br />    nCount++;<br />    delete (lpCacheEntry);<br /><br />    do <br />    {<br />        dwEntrySize = MAX_CACHE_ENTRY_INFO_SIZE;<br />        lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize];<br />        lpCacheEntry-&gt;dwStructSize = dwEntrySize;<br /><br />retry:<br />        if (!FindNextUrlCacheEntry(hCacheDir,<br />                                   lpCacheEntry, <br />                                   &amp;dwEntrySize))<br />        {<br />            delete[]lpCacheEntry;<br />            switch(GetLastError())<br />            {<br />                case ERROR_NO_MORE_ITEMS: <br />                    TCHAR tempout[80];<br />                    _stprintf_s(tempout,<br />                                80,<br />                                TEXT("The number of cache entries = %d \n"),nCount);<br />                    MessageBox(hX,<br />                               tempout,<br />                               TEXT("Cache Enumeration"),MB_OK);<br />                    FindCloseUrlCache(hCacheDir);<br />                    return TRUE;<br />                    break;<br />                case ERROR_INSUFFICIENT_BUFFER:<br />                    lpCacheEntry = <br />                             (LPINTERNET_CACHE_ENTRY_INFO) <br />                              new char[dwEntrySize];<br />                    lpCacheEntry-&gt;dwStructSize = dwEntrySize;<br />                    goto retry;<br />                    break;<br />                default:<br />                    ErrorOut(hX,<br />                             GetLastError(),<br />                             TEXT("FindNextUrlCacheEntry Init"));<br />                    FindCloseUrlCache(hCacheDir);<br />                    return FALSE;<br />            }<br />        }<br /><br />        SendDlgItemMessage(hX,<br />                           IDC_CacheList,LB_ADDSTRING,<br />                           0,<br />                          (LPARAM)(lpCacheEntry-&gt;lpszSourceUrlName));<br />        nCount++;<br />        delete[] lpCacheEntry;        <br />    }  while (TRUE);<br /><br />    SetCursor(LoadCursor(NULL,IDC_ARROW));<br />    return TRUE;        <br />}<br /><br /><br /></pre>
								</div>
						</div>
				</div>
		</div>
		<h4>Retrieving Cache Entry Information
      
     </h4>
		<p>The 
<a href="http://msdn.microsoft.com/en-us/library/aa384185.aspx"><strong>GetUrlCacheEntryInfo</strong></a> function lets you retrieve the 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a>
 structure for the specified URL. This structure contains the structure 
size, URL of the cached information, local file name, cache entry type, 
use count, hit rate, size, last modified time, expiration, last access, 
last synchronized time, header information, header information size, and
 file name extension. </p>
		<p>
				<a href="http://msdn.microsoft.com/en-us/library/aa384185.aspx">
						<strong>GetUrlCacheEntryInfo</strong>
				</a> accepts a URL, a buffer for an 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a>
 structure, and the buffer size. If the URL is found, the information is
 copied into the buffer. Otherwise, the function fails and 
<a href="http://msdn.microsoft.com/en-us/library/ms679360.aspx"><strong>GetLastError</strong></a> returns ERROR_FILE_NOT_FOUND. If the buffer size is insufficient to store the cache entry information, the function fails and 
<a href="http://msdn.microsoft.com/en-us/library/ms679360.aspx"><strong>GetLastError</strong></a> returns ERROR_INSUFFICIENT_BUFFER. The size required to retrieve the information is stored in the buffer size variable. </p>
		<p>
				<a href="http://msdn.microsoft.com/en-us/library/aa384185.aspx">
						<strong>GetUrlCacheEntryInfo</strong>
				</a>
 does not do any URL parsing, so a URL that contains an anchor (#) will 
not be found in the cache, even if the resource is cached. For example, 
if the URL "http://example.com/example.htm#sample" is passed, the 
function returns ERROR_FILE_NOT_FOUND even if 
"http://example.com/example.htm" is in the cache. </p>
		<p>The following example retrieves the cache entry information for the 
specified URL. The function then displays the header information in the <strong>IDC_CacheDump</strong> edit box. </p>
		<div class="LW_CodeSnippetContainer">
				<a name="CodeSpippet1">
				</a>
				<div class="LW_CodeSnippetContainerCodeCollection">
						<div class="LW_CodeSnippetToolBar">
								<div class="LW_CodeSnippetToolBarText" style="">
										<a name="CodeSnippetname" style="display: none;" title="Copy to clipboard.">Copy</a>
								</div>
						</div>
						<div id="CodeSnippetContainerCode1" class="LW_CodeSnippetContainerCode">
								<div style="color: Black;">
										<pre>
												<br />int WINAPI GetCacheEntryInfo(HWND hX,LPTSTR lpszUrl)<br />{<br />    DWORD dwEntrySize=0;<br />    LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry;<br /><br />    SetCursor(LoadCursor(NULL,IDC_WAIT));<br />    if (!GetUrlCacheEntryInfo(lpszUrl,NULL,&amp;dwEntrySize))<br />    {<br />        if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER)<br />        {<br />            ErrorOut(hX,GetLastError(),TEXT("GetUrlCacheEntryInfo"));<br />            SetCursor(LoadCursor(NULL,IDC_ARROW));<br />            return FALSE;<br />        }<br />        else<br />            lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) <br />                            new char[dwEntrySize];<br />    }<br />    else<br />        return FALSE; // should not be successful w/ NULL buffer<br />                      // and 0 size<br /><br />    if (!GetUrlCacheEntryInfo(lpszUrl,lpCacheEntry,&amp;dwEntrySize))<br />    {<br />        ErrorOut(hX,GetLastError(),TEXT("GetUrlCacheEntryInfo"));<br />        SetCursor(LoadCursor(NULL,IDC_ARROW));<br />        return FALSE;<br />    }<br />    else<br />    {<br />        if ((lpCacheEntry-&gt;dwHeaderInfoSize)!=0)<br />        {<br />            LPSTR(lpCacheEntry-&gt;lpHeaderInfo)<br />                                [lpCacheEntry-&gt;dwHeaderInfoSize]=TEXT('\0');<br />            SetDlgItemText(hX,IDC_Headers,<br />                           lpCacheEntry-&gt;lpHeaderInfo);<br />        }<br />        else<br />        {<br />            SetDlgItemText(hX,IDC_Headers,TEXT("None"));<br />        }<br /><br />        SetCursor(LoadCursor(NULL,IDC_ARROW));<br />        return TRUE;<br />    }<br /><br />}<br /><br /><br /></pre>
								</div>
						</div>
				</div>
		</div>
		<h4>Creating a Cache Entry
      
     </h4>
		<p>An application uses the 
<a href="http://msdn.microsoft.com/en-us/library/aa383968.aspx"><strong>CreateUrlCacheEntry</strong></a> and 
<a href="http://msdn.microsoft.com/en-us/library/ff384269.aspx"><strong>CommitUrlCacheEntry</strong></a> functions to create a cache entry. </p>
		<p>
				<a href="http://msdn.microsoft.com/en-us/library/aa383968.aspx">
						<strong>CreateUrlCacheEntry</strong>
				</a>
 accepts the URL, expected file size, and file name extension. The 
function then creates a local file name for saving the cache entry that 
corresponds to the URL and file name extension. </p>
		<p>Using the local file name, write the data into the local file. After 
the data has been written to the local file, the application should call
 
<a href="http://msdn.microsoft.com/en-us/library/ff384269.aspx"><strong>CommitUrlCacheEntry</strong></a>.</p>
		<p>
				<a href="http://msdn.microsoft.com/en-us/library/ff384269.aspx">
						<strong>CommitUrlCacheEntry</strong>
				</a>
 accepts the URL, local file name, expiration, last modified time, cache
 entry type, header information, header information size, and file name 
extension. The function then caches data in the file specified in the 
cache storage and associates it with the given URL.</p>
		<p>The following example uses the local file name, created by a previous call to 
<a href="http://msdn.microsoft.com/en-us/library/aa383968.aspx"><strong>CreateUrlCacheEntry</strong></a>, stored in the text box, <strong>IDC_LocalFile</strong>, to store the text from the text box, <strong>IDC_CacheDump</strong>, in the cache entry. After the data has been written to the file using <strong>fopen</strong>, <strong>fprintf</strong>, and <strong>fclose</strong>, the entry is committed using 
<a href="http://msdn.microsoft.com/en-us/library/ff384269.aspx"><strong>CommitUrlCacheEntry</strong></a>.</p>
		<div class="LW_CodeSnippetContainer">
				<a name="CodeSpippet2">
				</a>
				<div class="LW_CodeSnippetContainerCodeCollection">
						<div class="LW_CodeSnippetToolBar">
								<div class="LW_CodeSnippetToolBarText" style="">
										<a name="CodeSnippetname" style="display: none;" title="Copy to clipboard.">Copy</a>
								</div>
						</div>
						<div id="CodeSnippetContainerCode2" class="LW_CodeSnippetContainerCode">
								<div style="color: Black;">
										<pre>int WINAPI CommitEntry(HWND hX)<br />{<br />    LPTSTR lpszUrl, lpszExt, lpszFileName;<br />    LPTSTR lpszData,lpszSize;<br />    DWORD dwSize;<br />    DWORD dwEntryType=0;<br />    FILE *lpfCacheEntry;<br />    LPFILETIME lpdtmExpire, lpdtmLastModified;<br />    LPSYSTEMTIME lpdtmSysTime;<br />    errno_t err;<br /><br />    if( SendDlgItemMessage(hX,IDC_RBNormal,BM_GETCHECK,0,0) )<br />    {<br />        dwEntryType = dwEntryType + NORMAL_CACHE_ENTRY;<br />    }<br />    else if( SendDlgItemMessage(hX,IDC_RBSticky, BM_GETCHECK,0,0) )<br />    {<br />        dwEntryType = dwEntryType + STICKY_CACHE_ENTRY;<br />    }<br />    else if(SendDlgItemMessage( hX,IDC_RBSparse, BM_GETCHECK,0,0) )<br />    {<br />        dwEntryType = dwEntryType + SPARSE_CACHE_ENTRY;<br />    }<br /><br /><br />    if( SendDlgItemMessage(hX,IDC_RBCookie, BM_GETCHECK,0,0))<br />    {<br />            dwEntryType = dwEntryType + COOKIE_CACHE_ENTRY;<br />    }<br />    else if( SendDlgItemMessage(hX,IDC_RBUrl, BM_GETCHECK,0,0) )<br />    {<br />        dwEntryType = dwEntryType + URLHISTORY_CACHE_ENTRY;<br />    }<br /><br /><br />    if( SendDlgItemMessage(hX,IDC_RBNone, BM_GETCHECK,0,0) )<br />    {<br />        dwEntryType=0;<br />    }<br /><br />    lpdtmSysTime = new SYSTEMTIME;<br />    lpdtmExpire = new FILETIME;<br />    lpdtmLastModified = new FILETIME;<br /><br />    GetLocalTime(lpdtmSysTime);<br />    SystemTimeToFileTime(lpdtmSysTime,lpdtmExpire);<br />    SystemTimeToFileTime(lpdtmSysTime,lpdtmLastModified);<br />    delete(lpdtmSysTime);<br /><br />    lpszUrl = new TCHAR[MAX_PATH];<br />    lpszFileName = new TCHAR[MAX_PATH];<br />    lpszExt = new TCHAR[5];<br />    lpszSize = new TCHAR[10];<br /><br />    GetDlgItemText(hX,IDC_SourceURL,lpszUrl,MAX_PATH);<br />    GetDlgItemText(hX,IDC_LocalFile,lpszFileName,MAX_PATH);<br />    GetDlgItemText(hX,IDC_FileExt,lpszExt,5);<br /><br />    GetDlgItemText(hX,IDC_SizeLow,lpszSize,10);<br />    dwSize = (DWORD)_ttol(lpszSize);<br />    delete(lpszSize);<br /><br />    if (dwSize==0)<br />    {<br />        if((MessageBox(hX,<br />                       TEXT("Incorrect File Size.\nUsing 8000 characters, Okay?\n"),<br />                       TEXT("Commit Entry"),MB_YESNO))<br />                        ==IDYES)<br />        {<br />            dwSize = 8000;<br />        }<br />        else<br />        {<br />            return FALSE;<br />        }<br />    }<br /><br />    lpszData = new TCHAR[dwSize];<br />    GetDlgItemText(hX,IDC_CacheDump,lpszData,dwSize);<br /><br />     err = _tfopen_s(&amp;lpfCacheEntry,lpszFileName,_T("w"));<br />     if (err)<br />        return FALSE;<br />    fprintf(lpfCacheEntry,"%s",lpszData);<br />    fclose(lpfCacheEntry);<br />    delete(lpszData);<br /><br />    if ( !CommitUrlCacheEntry( lpszUrl, <br />                               lpszFileName, <br />                               *lpdtmExpire,<br />                               *lpdtmLastModified, <br />                               dwEntryType,<br />                               NULL,<br />                               0,<br />                               lpszExt,<br />                               0) )<br />    {<br />        ErrorOut(hX,GetLastError(),TEXT("Commit Cache Entry"));<br />        delete(lpszUrl);<br />        delete(lpszFileName);<br />        delete(lpszExt);<br />        delete(lpdtmExpire);<br />        delete(lpdtmLastModified);<br />        return FALSE;<br />    }<br />    else<br />    {<br />        delete(lpszUrl);<br />        delete(lpszFileName);<br />        delete(lpszExt);<br />        delete(lpdtmExpire);<br />        delete(lpdtmLastModified);<br />        return TRUE;<br />    }<br />}<br /><br /><br /></pre>
								</div>
						</div>
				</div>
		</div>
		<h4>Deleting a Cache Entry</h4>
		<p>The 
<a href="http://msdn.microsoft.com/en-us/library/aa383983.aspx"><strong>DeleteUrlCacheEntry</strong></a> function takes a URL and removes the cache file associated with it. If the cache file does not exist, the function fails and 
<a href="http://msdn.microsoft.com/en-us/library/ms679360.aspx"><strong>GetLastError</strong></a> returns ERROR_FILE_NOT_FOUND. If the cache file is currently locked or in use, the function fails and 
<a href="http://msdn.microsoft.com/en-us/library/ms679360.aspx"><strong>GetLastError</strong></a> returns ERROR_ACCESS_DENIED. The file is deleted when unlocked.</p>
		<h4>Retrieving Cache Entry Files</h4>
		<p>For applications that require the file name of a resource, use the  
<a href="http://msdn.microsoft.com/en-us/library/aa385365.aspx"><strong>RetrieveUrlCacheEntryFile</strong></a> and 
<a href="http://msdn.microsoft.com/en-us/library/aa385407.aspx"><strong>UnlockUrlCacheEntryFile</strong></a> functions. Applications that do not require the file name should use the 
<a href="http://msdn.microsoft.com/en-us/library/aa385368.aspx"><strong>RetrieveUrlCacheEntryStream</strong></a>, 
<a href="http://msdn.microsoft.com/en-us/library/aa385354.aspx"><strong>ReadUrlCacheEntryStream</strong></a>, and 
<a href="http://msdn.microsoft.com/en-us/library/aa385415.aspx"><strong>UnlockUrlCacheEntryStream</strong></a> functions to retrieve the information in the cache.</p>
		<p>
				<a href="http://msdn.microsoft.com/en-us/library/aa385368.aspx">
						<strong>RetrieveUrlCacheEntryStream</strong>
				</a>
 does not do any URL parsing, so a URL that contains an anchor (#) will 
not be found in the cache, even if the resource is cached. For example, 
if the URL "http://example.com/example.htm#sample" is passed, the 
function returns ERROR_FILE_NOT_FOUND even if 
"http://example.com/example.htm" is in the cache.</p>
		<p>
				<a href="http://msdn.microsoft.com/en-us/library/aa385365.aspx">
						<strong>RetrieveUrlCacheEntryFile</strong>
				</a> accepts a URL, a buffer that stores the 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a> structure, and the buffer size. The function is retrieved and locked for the caller.</p>
		<p>After the information in the file has been used, the application should call 
<a href="http://msdn.microsoft.com/en-us/library/aa385407.aspx"><strong>UnlockUrlCacheEntryFile</strong></a> to unlock the file.</p>
		<h4>Cache Groups</h4>
		<p>To create a cache group, the 
<a href="http://msdn.microsoft.com/en-us/library/aa383972.aspx"><strong>CreateUrlCacheGroup</strong></a> function must be called to generate a <strong>GROUPID</strong>
 for the cache group. Entries can be added to the cache group by 
supplying the cache entry's URL and the INTERNET_CACHE_GROUP_ADD flag to
 the 
<a href="http://msdn.microsoft.com/en-us/library/aa385390.aspx"><strong>SetUrlCacheEntryGroup</strong></a> function. To remove a cache entry from a group, pass the cache entry's URL and the INTERNET_CACHE_GROUP_REMOVE flag to 
<a href="http://msdn.microsoft.com/en-us/library/aa385390.aspx"><strong>SetUrlCacheEntryGroup</strong></a>.</p>
		<p>The 
<a href="http://msdn.microsoft.com/en-us/library/aa384034.aspx"><strong>FindFirstUrlCacheEntryEx</strong></a> and 
<a href="http://msdn.microsoft.com/en-us/library/aa384057.aspx"><strong>FindNextUrlCacheEntryEx</strong></a>
 functions can be used to enumerate the entries in a specified cache 
group. After the enumeration is complete, the function should call 
<a href="http://msdn.microsoft.com/en-us/library/aa384015.aspx"><strong>FindCloseUrlCache</strong></a>.</p>
		<h3>Handling Structures with Variable Size Information</h3>
		<p>The cache can contain variable size information for each URL stored. This is reflected in the 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a> structure. When the cache functions return this structure, they create a buffer that is always the size of 
<a href="http://msdn.microsoft.com/en-us/library/aa385134.aspx"><strong>INTERNET_CACHE_ENTRY_INFO</strong></a>
 plus any variable size information. If a pointer member is not NULL, it
 points to the memory area immediately after the structure. While 
copying the buffer returned by a function into another buffer, the 
pointer members should be fixed to point to the appropriate place in the
 new buffer, as the following example shows.</p>
		<div class="LW_CodeSnippetContainer">
				<a name="CodeSpippet3">
				</a>
				<div class="LW_CodeSnippetContainerCodeCollection">
						<div class="LW_CodeSnippetToolBar">
								<div class="LW_CodeSnippetToolBarText" style="">
										<a name="CodeSnippetname" style="display: none;" title="Copy to clipboard.">Copy</a>
								</div>
						</div>
						<div id="CodeSnippetContainerCode3" class="LW_CodeSnippetContainerCode">
								<div style="color: Black;">
										<pre>lpDstCEInfo-&gt;lpszSourceUrlName = <br />    (LPINTERNET_CACHE_ENTRY_INFO) ((LPBYTE) lpSrcCEInfo + <br />       ((DWORD)(lpOldCEInfo-&gt;lpszSourceUrlName) - (DWORD)lpOldCEInfo));<br /><br /></pre>
								</div>
						</div>
				</div>
		</div>
		<p>Some cache functions fail with the ERROR_INSUFFICIENT_BUFFER error 
message if you specify a buffer that is too small to contain the cache 
entry information retrieved by the function. In this case, the function 
also returns the required size of the buffer. You can then allocate a 
buffer of the appropriate size and call the function again.</p>
		<p class="note">
				<strong>Note</strong>  WinINet does not support server 
implementations. In addition, it should not be used from a service.  For
 server implementations or services use <a href="http://msdn.microsoft.com/en-us/library/aa384273.aspx">Microsoft Windows HTTP Services (WinHTTP)</a>.</p>
		<p> </p>
		<p> </p>
		<p>
				<a href="mailto:wsddocfb@microsoft.com?subject=Documentation%20feedback%20[wininet%5Cwininet]:%20Caching%20%20RELEASE:%20%286/2/2011%29&amp;body=%0A%0APRIVACY%20STATEMENT%0A%0AThe%20SDK%20team%20uses%20the%20feedback%20submitted%20to%20improve%20the%20SDK%20documentation.%20We%20do%20not%20use%20your%20email%20address%20for%20any%20other%20purpose.%20We%20will%20remove%20your%20email%20address%20from%20our%20system%20after%20the%20issue%20you%20are%20reporting%20has%20been%20resolved.%20While%20we%20are%20working%20to%20resolve%20this%20issue,%20we%20may%20send%20you%20an%20email%20message%20to%20request%20more%20information%20about%20your%20feedback.%20After%20the%20issues%20have%20been%20addressed,%20we%20may%20send%20you%20an%20email%20message%20to%20let%20you%20know%20that%20your%20feedback%20has%20been%20addressed.%0A%0AFor%20more%20information%20about%20Microsoft%27s%20privacy%20policy,%20see%20http://privacy.microsoft.com/en-us/default.aspx." title="Send comments about this topic to Microsoft">Send comments about this topic to Microsoft</a>
		</p>
<img src ="http://www.cppblog.com/msopengl/aggbug/148427.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/msopengl/" target="_blank">Skill</a> 2011-06-10 15:10 <a href="http://www.cppblog.com/msopengl/articles/148427.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>