⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blobcache.h

📁 Vc.Net入门与提高源码
💻 H
字号:
// BlobCache.h : Defines the ATL Server request handler class
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Classes Reference and related electronic
// documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft C++ Libraries products.

#pragma once

[ request_handler("Default") ]
class CBlobCacheHandler
{
private:
	// Put private members here
	// uncomment the service declaration(s) if you want to use
	// a service that was generated with your ISAPI extension

	// Blob cache support
	CComPtr<IMemoryCache> m_cache;
	char value[100];
	char status[100];
//	char stats[100];

public:
	HTTP_CODE ValidateAndExchange()
	{
		m_HttpResponse.SetContentType("text/html");

		const CHttpRequestParams& formFields = m_HttpRequest.GetFormVars();

		CHttpRequest::HTTP_METHOD m = m_HttpRequest.GetMethod();

		if ( m == CHttpRequest::HTTP_METHOD_GET ) // HTTP_METHOD_GET means that this handler is being loaded
		{
			*value = 0;
            *status = 0; 
//			*stats = 0;
		}
		else
		{
			*value = 0;
			*status = 0;

			LPCTSTR clear = formFields.Lookup("Clear");
			if (clear)
			{
				CComPtr<IMemoryCacheControl> control;
				if (SUCCEEDED(m_spServiceProvider->QueryService(__uuidof(IMemoryCacheControl), &control)))
				{
					control->ResetCache();
					strcpy( status, "cache cleared" );
				}
				else
					strcpy( status, "cache clear failed" );

				*value = 0;

				return HTTP_SUCCESS;
			}

			LPCTSTR keyRaw = formFields.Lookup("keystr");

			if (keyRaw)
			{
				if (strlen(keyRaw) == 0)
				{
					strcpy( status, "no key entered" );
					*value = 0;
					return HTTP_SUCCESS;
				}

				const char* p = keyRaw;
				while (*p)
				{
					if (*p < '0' || *p > '9')
					{
						strcpy( status, "invalid key" );
						*value = 0;
						return HTTP_SUCCESS;
					}
					p++;
				}

				int keyVal = atoi( keyRaw );
				if (keyVal < 1 || keyVal > 100)
				{
					strcpy( status, "key out of range" );
					*value = 0;
					return HTTP_SUCCESS;
				}
				
				// keyRaw is ok, now try to look is up in cache
    
				char keyStr[50];
				sprintf( keyStr, "%d", keyVal );

				m_spServiceProvider->QueryService(__uuidof(IMemoryCache), &m_cache);
				ATLASSERT(m_cache);

				HCACHEITEM hEntry;
				HRESULT r = m_cache->LookupEntry( keyStr, &hEntry );
//				if (false)
				if (SUCCEEDED(r))
				{
					DWORD size = 0;
					void* data = 0;
					if (SUCCEEDED(m_cache->GetData( hEntry, &data, &size )))
					{
						memcpy( value, data, size );
						value[size] = 0;
						if (*value == 0)
							strcpy( status, "BAD VALUE" );
						else
							strcpy( status, "cached response");
					}
					else
					{
						strcpy( value, "??" );
						strcpy( status, "cache->GetData() failed" );
					}
				}
				else
				{
					CStringA dataFile;
					m_HttpRequest.GetPhysicalPath( dataFile );
					dataFile += "values.txt";
					GetValueFromFile( dataFile, keyVal, value );

					FILETIME ft = {0,0};
					m_cache->Add( keyStr, value, strlen(value), &ft, 0, &hEntry, 0 );
					strcpy( status, "not cached" );
				}

				return HTTP_SUCCESS;
			}
			else
			{
				strcpy( status, "(fail)" );
				*value = 0;
				return HTTP_SUCCESS;
			}
		}


		return HTTP_SUCCESS;
	}
 
	HRESULT GetValueFromFile(const CStringA& fileName, int index, char* val)
	{
		char str[100];

//		LPCTSTR path = GetPathTranslated();

		FILE* fp = fopen( fileName, "r" );
		if (fp == 0)
		{
			*val = 0;
			return E_FAIL;
		}
		else
		{
			for (int i=0; i<index; i++)
			{
				fgets( str, 100, fp );
			}
		}

		fclose( fp );

		char* p = str;
		while (*p !=0)
		{
			if (*p == 10)
				*p = 0;
			p++;
		}

		strcpy( val, str );

		return S_OK;
	}

protected:
	// Here is an example of how to use a replacement tag with the stencil processor
	[ tag_name(name="output") ]
	HTTP_CODE OnOutput(void)
	{
		if (*value)
		{
			m_HttpResponse << "<tr>";
			m_HttpResponse << "<td><b>value</b></td><td>" << value << "</td>";
			m_HttpResponse << "</tr>";
		}

		if (*status)
		{
			m_HttpResponse << "<tr>";
			m_HttpResponse << "<td><b>status</b></td><td>" << status << "</td>";
			m_HttpResponse << "</tr>";
		}
						
		return HTTP_SUCCESS;
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -