influxweb
3/16/2016 - 2:52 PM

Transient Module

Transient Module

<MvFUNCTION NAME = "Module_Description" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "compresswhitespace">
	<MvASSIGN NAME = "l.module:code"		VALUE = "tg_transients">
	<MvASSIGN NAME = "l.module:name"		VALUE = "Transients">
	<MvASSIGN NAME = "l.module:provider"	VALUE = "Tess Guefen">
	<MvASSIGN NAME = "l.module:version"		VALUE = "1.005">
	<MvASSIGN NAME = "l.module:api_ver"		VALUE = "9.00">
	<MvASSIGN NAME = "l.module:description"	VALUE = "Save Cached versions of data, set an expiration time, and be happy.">
	<MvASSIGN NAME = "l.module:features"	VALUE = "util, vis_util, data_store, json, clientside">
</MvFUNCTION>

<MvCOMMENT>
| ========================================================================================================================
|	INSTALL
| ========================================================================================================================
</MvCOMMENT>

<MvFUNCTION NAME = "Module_Install_Store" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
	<MvCOMMENT>
	===========================
	Create Transients Table
	===========================
	</MvCOMMENT>

	<MvQUERY NAME	= "Merchant"
			 QUERY	= "{ 'CREATE TABLE ' $  g.Store_Table_Prefix $ 'Transients
						  (
							tkey		' $ [ g.Module_Library_Native_DBAPI ].DB_Type_CHAR( 255 )		$ ',
							expires	' $ [ g.Module_Library_Native_DBAPI ].DB_Type_NUMBER( 0, 255 )	$ ',
							tvalue	' $ [ g.Module_Library_Native_DBAPI ].DB_Type_MEMO()			$ '
						  ) ' }">
	<MvIF EXPR = "{ g.MvQUERY_Error }">
		<MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_Utilities ].Error( 'TRANSIENTS-INSTALL-1001:', 'An error occured while creating the table Transients. Please make sure this table was not already created.' ) }">
	</MvIF>

	<MvQUERY NAME	= "Merchant"
			 QUERY	= "{ 'CREATE UNIQUE INDEX ' $ g.Store_Table_Prefix $ 'Transients ON ' $ g.Store_Table_Prefix $ 'Transients ( tkey )' }">
	<MvIF EXPR = "{ g.MvQUERY_Error }">
		<MvQUERY NAME = "Merchant" QUERY = "{ 'DROP TABLE ' $  g.Store_Table_Prefix $ 'Transients' }">
		<MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_Utilities ].Error( 'TRANSIENTS-INSTALL-1002:', g.MvQUERY_Error ) }">
	</MvIF>
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "Module_Upgrade_Store" PARAMETERS = "module var, version" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "Module_Uninstall_Store" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "">
	<MvQUERY NAME = "Merchant" QUERY = "{ 'DROP TABLE ' $  g.Store_Table_Prefix $ 'Transients' }">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvCOMMENT>
| ========================================================================================================================
|	VIS_UTIL
| ========================================================================================================================
</MvCOMMENT>

<MvFUNCTION NAME = "StoreUtilityModule_Action" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "StoreUtilityModule_LeftNavigation" PARAMETERS = "module var, ident" STANDARDOUTPUTLEVEL = "compresswhitespace">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "StoreUtilityModule_Screen" PARAMETERS = "module_var" STANDARDOUTPUTLEVEL = "compresswhitespace">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "StoreUtilityModule_Validate" PARAMETERS = "module_var" STANDARDOUTPUTLEVEL = "compresswhitespace">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>


<MvCOMMENT>
| ========================================================================================================================
|	VIS_UTIL
| ========================================================================================================================
</MvCOMMENT>

<MvFUNCTION NAME = "Module_Utility_Content" PARAMETERS = "module var, tab, load_fields" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
	<MvIF EXPR = "{ l.tab EQ 'TRANSIENTS' }">
		<MvEVAL EXPR = "{ [ g.Module_Admin ].Element_MMBatchList_HTML() }">
		<div id="AllTransients"></div>
	</MvIF>
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "Module_Utility_Head" PARAMETERS = "module var, tab" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
	<MvIF EXPR = "{ l.tab EQ 'TRANSIENTS' }">
		<MvEVAL EXPR = "{ [ g.Module_Admin ].Element_MMBatchList_JavaScript() }">
		<MvEVAL EXPR = "{ [ g.Module_Admin ].Element_MMBatchList_CSS() }">
		<script language="JavaScript" src="{ g.clientside_url $ 'Module_Code=' $ encodeattribute( l.module:code ) $ '&Filename=AllTransients_functions.js' }"></script>
		<script language="JavaScript" src="{ g.clientside_url $ 'Module_Code=' $ encodeattribute( l.module:code ) $ '&Filename=AllTransients.js' }"></script>
		<script language="JavaScript">
			MMScreen_LoadFinished( function() { new AllTransients_Batchlist(); } );
		</script>
	</MvIF>
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "Module_Utility_Tabs" PARAMETERS = "module_var" STANDARDOUTPUTLEVEL = "compresswhitespace">
	<MvFUNCTIONRETURN VALUE = "TRANSIENTS:Transients" />
</MvFUNCTION>

<MvFUNCTION NAME = "Module_Utility_Update" PARAMETERS = "module_var" STANDARDOUTPUTLEVEL = "compresswhitespace">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "Module_Utility_Validate" PARAMETERS = "module_var" STANDARDOUTPUTLEVEL = "compresswhitespace">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvCOMMENT>
| ========================================================================================================================
|	CLIENTSIDE
| ========================================================================================================================
</MvCOMMENT>

<MvFUNCTION NAME = "Module_Clientside" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "text, compresswhitespace">
	<MvIF EXPR = "{ g.Filename EQ 'AllTransients_functions.js' }">
		<MvINCLUDE FILE = "js/AllTransients_functions.js">
	</MvIF>
	<MvIF EXPR = "{ g.Filename EQ 'AllTransients.js' }">
		<MvINCLUDE FILE = "js/AllTransients.js">
	</MvIF>
</MvFUNCTION>


<MvCOMMENT>
| ========================================================================================================================
|	JSON
| ========================================================================================================================
</MvCOMMENT>

<MvFUNCTION NAME = "Module_JSON" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "text, compresswhitespace">
	<MvIF EXPR = "{ g.Module_Function EQ 'AllTransients_Load_Query' }">
		<MvEVAL EXPR = "{ Batchlist_Transients( l.module) }">
	</MvIF>
	<MvIF EXPR = "{ g.Module_Function EQ 'Delete_Transient' }">
		<MvIF EXPR = "{ Delete_Transient( trim( g.Transient_Key ) ) }">
			{
				"success": 1
			}
		<MvELSE>
			{
				"success": 0
			}
		</MvIF>
	</MvIF>
</MvFUNCTION>

<MvCOMMENT>
| ========================================================================================================================
|	MODULE FUNCTIONS
|	1. Get_Transient(key)
|	2. Set_Transient(key, value, expires)
|	3. Delete_Transient(key)
|	4. Batchlist_Transients
| ========================================================================================================================
</MvCOMMENT>

<MvFUNCTION NAME = "Get_Transient" PARAMETERS = "key" STANDARDOUTPUTLEVEL = "">
	<MvIF EXPR = "{ ISNULL l.key }">
		<MvFUNCTIONRETURN VALUE = "" />
	</MvIF>

	<MvIF EXPR = "{ NOT Get_Transient_LowLevel( l.key, l.output ) }">
		<MvFUNCTIONRETURN VALUE = "" />
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.output }">
</MvFUNCTION>

<MvFUNCTION NAME = "Get_Transient_LowLevel" PARAMETERS = "key, output var" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "">
	<MvOPENVIEW	NAME	= "Merchant"
					VIEW 	= "GetTransients"
					QUERY 	= "{ 'SELECT
									*
								FROM ' $
									g.Store_Table_Prefix $ 'Transients
								WHERE
									tkey = ?'
								}"
					FIELDS	= "l.key">

	<MvIF EXPR = "{ g.MvOPENVIEW_Error }">
		<MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_Utilities ].Error( 'TRANSIENTS-MOD-2000:', 'An error occurred while retrieving the requestest Transient.' ) }">
	</MvIF>

	<MvIF EXPR = "{ GetTransients.d.EOF }">
		<MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_DB ].Error_Load_EOF( '#Error# Could Not Find KEY' ) }">
	</MvIF>

	<MvASSIGN NAME = "l.transient:key"		VALUE = "{ GetTransients.d.tkey }">
	<MvASSIGN NAME = "l.transient:expires"	VALUE = "{ GetTransients.d.expires }">
	<MvASSIGN NAME = "l.transient:value"	VALUE = "{ GetTransients.d.tvalue }">

	<MvCLOSEVIEW NAME = "Merchant" VIEW = "GetTransients">

	<MvCOMMENT> === [ Check if Transient is expired ] === </MvCOMMENT>
	<MvIF EXPR = "{ s.dyn_time_t GT l.transient:expires }">
		<MvASSIGN NAME = "l.success" VALUE = "{ Delete_Transient( l.transient:key ) }">
	</MvIF>

	<MvASSIGN NAME = "l.output" VALUE = "{ l.transient:value }">

	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "Set_Transient" PARAMETERS = "key, value, expires" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.transient" VALUE = "{ Get_Transient( l.key ) }">

	<MvIF EXPR = "{ NOT trim( l.value ) OR NOT trim( l.key ) }">
		<MvFUNCTIONRETURN VALUE = 1 />
	</MvIF>

	<MvIF EXPR = "{ l.transient }">
		<MvASSIGN NAME = "l.success" VALUE = "{  Delete_Transient( l.key ) }">
	</MvIF>

	<MvASSIGN NAME = "l.expires" VALUE = "{ l.expires + s.dyn_time_t }">

	<MvQUERY	NAME 	= "Merchant"
				QUERY 	= "{ 'INSERT INTO ' $ g.Store_Table_Prefix $ 'Transients
								( tkey, tvalue, expires )
								VALUES
								(?, ?, ?)'}"
			 	FIELDS = "l.key, l.value, l.expires">
	<MvIF EXPR = "{ g.MvQUERY_Error }">
		<MvFUNCTIONRETURN VALUE = "{ [ g.Module_Library_Utilities ].Error( 'QNA-MOD-2017', g.MvQUERY_Error ) }">
	</MvIF>
</MvFUNCTION>

<MvFUNCTION NAME = "Delete_Transient" PARAMETERS = "key" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "">
	<MvQUERY	NAME 	= "Merchant"
				QUERY 	= "{ 'DELETE FROM ' $ g.Store_Table_Prefix $ 'Transients WHERE tkey = ?' }"
			 	FIELDS = "l.key">
	<MvFUNCTIONRETURN VALUE = 1 />
</MvFUNCTION>

<MvFUNCTION NAME = "Batchlist_Transients" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
	<MvASSIGN NAME = "g.Filter" VALUE = "{ trim( g.Filter ) }">
	<MvASSIGN NAME = "g.Sort" VALUE = "{ trim( g.Sort ) }">
	<MvASSIGN NAME = "g.Offset" VALUE = "{ trim( g.Offset ) }">
	<MvASSIGN NAME = "g.Count" VALUE = "{ trim( g.Count ) }">
	<MvASSIGN NAME = "l.search_query" VALUE = "">

	<MvEVAL EXPR = "{ [ g.Module_Library_DB ].SQL_Query_SELECT( l.search_query,'s.*' ) }">

	<MvEVAL EXPR = "{ [ g.Module_Library_DB ].SQL_Query_FROM( l.search_query, g.Store_Table_Prefix $ 'Transients', 's' ) }">

	<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_Filter( l.search_query, g.Filter,'tkey:s.tkey,tvalue:s.tvalue,expires:s.expires' ) }">

	<MvEVAL EXPR = "{ [ g.Module_Library_DB ].SQL_Query_OrderBy_Fields(l.search_query, g.Sort, 'tkey:s.tkey,tvalue:s.tvalue,expires:s.expires', 's.tkey' ) }">

	<MvASSIGN NAME = "l.search_sql" VALUE = "{ [ g.Module_Library_DB].SQL_Query_Build( l.search_query, l.search_fields ) }">

	<MvIF EXPR = "{ NOT [ g.Module_Library_DB ].SQL_Query_Count( l.search_query, l.total_count ) }">
		<MvFUNCTIONRETURN VALUE = "{ [ g.Module_JSON ].JSON_Response_Error(g.Error_Code, g.Error_Message ) }">
	<MvELSEIF EXPR = "{ NOT [ g.Module_Library_Native_DBAPI ].DB_OPENVIEW_Range('Merchant', 'Transients', l.search_sql, l.search_fields, g.Offset, g.Count) }">
		<MvFUNCTIONRETURN VALUE = "{ [ g.Module_JSON ].JSON_Response_Error( 'TRANSIENTS-MOD-2005',g.MvOPENVIEW_Error ) }">
	</MvIF>

	<MvASSIGN NAME = "l.count" VALUE = 0>

	<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_Response_Start() }">
		{
			"data":
				[
					<MvWHILE EXPR = "{ ( NOT Transients.d.EOF ) AND ( ( g.Count EQ 0 ) OR (l.count LT g.Count ) ) }">
					<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_ArrayElement_Start( l.count )}">
					"tkey": "<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_Encode( Transients.d.tkey ) }">",
					"tvalue": "<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_Encode( Transients.d.tvalue ) }">",
					"expires": "<MvEVAL EXPR = "{ int( Transients.d.expires ) }">",
					<MvASSIGN NAME = "l.fdt" VALUE = "{ [ g.Module_Library_Utilities].Format_Date( Transients.d.expires, s.miva_language ) }">
					<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.fdt" VALUE = "{ l.fdt $ ' ' $ Format_Time( Transients.d.expires, s.miva_language ) }">
					"f_expires": "<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_Encode( l.fdt ) }">"
					<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_ArrayElement_End() }">
					<MvSKIP NAME = "Merchant" VIEW = "Transients" ROWS = 1>
					</MvWHILE>
				],

			"total_count": <MvEVAL EXPR = "{ int( l.total_count ) }">,
			"start_offset": <MvEVAL EXPR = "{ int( g.Offset ) }">
		}
		<MvCLOSEVIEW NAME = "Merchant" VIEW = "Transients">
	}
</MvFUNCTION>
function AllTransients_List_Load_Query( filter, sort, offset, count, callback, delegator ) {
	return AJAX_Call_Module( callback,
							'admin',
							'tg_transients',
							'AllTransients_Load_Query',
							'&Filter=' + EncodeArray( filter ) +
							'&Sort=' + encodeURIComponent( sort ) +
							'&Offset=' + encodeURIComponent( offset ) +
							'&Count=' + encodeURIComponent( count ),
							delegator );
}

// On Delete
function AllTransients_Batchlist_Delete( tkey, callback, delegator ) {
	return AJAX_Call_Module( callback,
							'admin',
							'tg_transients',
							'Delete_Transient',
							'Transient_Key=' + encodeURIComponent( tkey ),
							delegator );
}
function AllTransients_Batchlist() {
	MMBatchList.call( this, 'AllTransients' );
	this.Feature_SearchBar_SetPlaceholderText( 'Search Transients...' );
	this.SetDefaultSort( 'tkey', '' );
	this.Feature_Delete_Enable('Delete Transient(s)');
}

DeriveFrom( MMBatchList, AllTransients_Batchlist );

AllTransients_Batchlist.prototype.onLoad = AllTransients_List_Load_Query;

AllTransients_Batchlist.prototype.onCreateRootColumnList = function() {
	var columnlist =
	[
		new MMBatchList_Column_Name( 'Key', 'tkey', 'tkey'),
		new MMBatchList_Column_TextArea( 'Value', 'Value', 'tvalue', 'tvalue' ),
		new MMBatchList_Column_Name( 'Expires', 'expires', 'expires'),
		new MMBatchList_Column_Name( 'Formatted Expires', 'f_expires', 'f_expires')
		.SetAdvancedSearchEnabled(false)
	];
	return columnlist;
}

// On Delete
AllTransients_Batchlist.prototype.onDelete = function( item, callback, delegator ) {
	AllTransients_Batchlist_Delete( item.record.tkey, callback, delegator );
}

Transients

A Transient is a simple way of storing cached data in the database temporarily, by giving it a custom name and a timeframe after which it will expire and be deleted.

Transients are useful when pinging another API (i.e. Wordpress, Instagram, twitter), and saving data for a set period of time.

Once the module is installed, you will find a tab under Utilities. This batch list is only used for debugging. You can view transients here, and delete them.

Please Note

To use this, you must use mvt:do, and having experience with mvt:do is recommended.

Usage

There are two main functions you can use:

Setting a Transient

Set_Transient(key, value, expiration)

Key: This should be unique. If this key already exists, it will overwrite the value you had before. It is recommended you use this as a code (example: recent_posts, recent_tweets)

Value: The value you are saving/ storing. If you are wanting to save a miva array, serialize this before saving it.

Expiration: This is the amount of seconds to save the transient.

Example Syntax of Set_Transient

<mvt:do file="g.Module_Root $ '/modules/util/transients.mvc'" name="l.set_transient" value="Set_Transient( 'recent_posts', l.settings:my_value_here, 60*60*2)" />

Getting a Transient

Get_Transient(key)

This will retrieve your transient. If you have a transient that has not expired, it will return the value. If it has expired, or does not exsist it will return 0.

Example Syntax of Get_Transient

<mvt:do file="g.Module_Root $ '/modules/util/transients.mvc'" name="l.settings:recent_posts" value="Get_Transient( 'recent_posts' )" />
<mvt:if expr="l.settings:recent_posts">
    <mvt:eval expr="l.settings:recent_posts" />
</mvt:if>

Example

This is an example of pulling a Blog's most recent posts, and displaying them on SFNT. In this case, the URL we are calling to will display the most recent posts.

<mvt:do file="g.Module_Root $ '/modules/util/transients.mvc'" name="l.settings:recent_posts" value="Get_Transient( 'recent_posts' )" />
<mvt:if expr="l.settings:recent_posts">
    <mvt:eval expr="l.settings:recent_posts" />
<mvt:else>
    <mvt:assign name="l.settings:recent_posts" value="''" />
    <mvt:assign name="g.blog_url" value="'http://www.mydomainname.com/blog/recent-posts/'" />
    <mvt:call action="g.blog_url" method="'POST'">
        <mvt:assign name="l.settings:recent_posts" value="l.settings:recent_posts $ s.callvalue" />
    </mvt:call>
    <mvt:do file="g.Module_Root $ '/modules/util/transients.mvc'" name="l.set_transient" value="Set_Transient( 'recent_posts', l.settings:recent_posts, 60*60*2)" />
    &mvt:recent_posts;
</mvt:if>