github.com/webability-go/xcore (go): Package xcore is a set of basic objects for programation (XCache for caches, XDataset for data sets, XLanguage for languages and XTemplate for templates). For GO, the actual existing code includes:  - XCache: Application Memory Caches for any purpose, with time control and quantity control of object in the cache and also check changes against original source. It is a thread safe cache.  - XDataset: Basic nested data structures for any purpose (template injection, configuration files, database records, etc).  - XLanguage: language dependent text tables for internationalization of code. The sources can be text or XML file definitions.  - XTemplate: template system with meta language to create complex documents (compatible with any text language, HTML, CSS, JS, PDF, XML, etc), heavily used on CMS systems and others.  It is already used on sites that serve more than 60 million pages a month (500 pages per second on pike hour) and can be used on multithreading environment safely.  XCache is a library to cache all the data you want into current application memory for a very fast access to the data. The access to the data support multithreading and concurrency. For the same reason, this type of cache is not persistent (if you exit the application) and cannot grow too much (as memory is the limit). However, you can control a timeout of each cache piece, and eventually a comparison function against a source (file, database, etc) to invalid the cache.  1. Declare a new XCache with NewXCache() function:  2. Fill in the cache:  Once you have declared the cache, you can fill it with anything you want. The main cache object is an interface{} so you can put here anything you need, from simple variables to complex structures. You need to use the Set function: Note the ID is always a string, so convert a database key to string if needed.  3. To use the cache, just ask for your entry with Get function:  4. To maintain the cache:  You may need Del function, to delete a specific entry (maybe because you deleted the record in database). You may also need Clean function to deletes a percentage of the cache, or Flush to deletes it all. The Verify function is used to check cache entries against their sources through the Validator function. Be very careful, if the cache is big or the Validator function is complex (maybe ask for a remote server information), the verification may be VERY slow and huge CPU use. The Count function gives some stats about the cache.  5. How to use Verify Function:  This function is recommended when the source is local and fast to check (for instance a language file or a template file). When the source is distant (other cluster database, any rpc source on another network, integration of many parts, etc), it is more recommended to create a function that will delete the cache when needed (on demand cache change).  The validator function is a func(id, time.Time) bool function. The first parameter is the ID entry in the cache, the second parameter the time of the entry was created. The validator function returns true is the cache is still valid, or false if it needs to be invalidated.  The XCache is thread safe. The cache can be limited in quantity of entries and timeout for data. The cache is automanaged (for invalid expired data) and can be cleaned partially or totally manually.  The XLanguage table of text entries can be loaded from XML file, XML string or normal text file or string. It is used to keep a table of id=value set of entries in any languages you need, so it is easy to switch between XLanguage instance based on the required language needed. Obviously, any XLanguage you load in any language should have the same id entries translated, for the same use.  1. loading:  You can load any file or XML string directly into the object.  1.1 The XML Format is:  NAMEOFTABLE is the name of your table entry, for example "loginform", "user_report", etc.  LG is the ISO-3369 2 letters language ID, for example "es" for spanish, "en" for english, "fr" for french, etc.  ENTRYNAME is the ID of the entry, for example "greating", "yourname", "submitbutton".  ENTRYVALUE is the text for your entry, for example "Hello", "You are:", "Save" if your table is in english.  1.2 The flat text format is:  ENTRYNAME is the ID of the entry, for example "greating", "yourname", "submitbutton".  ENTRYVALUE is the text for your entry, for example "Hello", "You are:", "Save" if your table is in english.  There is no name of table or language in this format (you "know" what you are loading).  The advantage to use XML format is to have more control over your language, and eventyally add attributes into your entries, for instance you may add attributes translated="yes/no", verified="yes/no", and any other data that your system could insert. The XLanguage will ignore those attributes loading the table.  2. creation:  To create a new XLanguage empty structure:  There are 4 functions to create the language from a file or string, flat text or XML text:  Then you can use the set of basic access functions:  SetName/SetLanguage functions are used to set the table name and language of the object (generally to build an object from scratch). GetName/GetLanguage functions are used to get the table name and language of the object (generally when you load it from some source). Set/Get/Del functions are used to add or modify a new entry, read an entry, or deletes an entry in the object.  1. Overview:  The XDataSet is a set of interfaces and basic classes ready-to-use to build a standard set of data optionally nested and hierarchical, that can be used for any purpose:  - Keep complex data in memory.  - Create JSON structures.  - Inject data into templates.  - Interchange database data (records set and record).  You can store into it generic supported data, as well as any complex interface structures:  - Int  - Float  - String  - Time  - Bool  - []Int  - []Float  - []Time  - []Bool  - XDataSetDef (anything extended with this interface)  - []String  - Anything else ( interface{} )  - XDataSetCollectionDef (anything extended with this interface)  The generic supported data comes with a set of functions to get/set those data directly into the XDataset.  Example:  Note that all references to XDataset and XDatasetCollection are pointers, always (to be able to modify the values of them).  2. XDatasetDef interface:  It is the interface to describe a simple set of data mapped as "name": value, where value can be of any type.  The interface implements a good amount of basic methods to get the value on various format such as GetString("name"), GetInt("name"), etc (see below).  If the value is another type as asked, the method should contert it if possible. For instance "key":123 required through GetString("key") should return "123".  The XDataset type is a simple map[string]interface{} with all the implemented methods and should be enough to use for almost all required cases.  However, you can build any complex structure that extends the interface and implements all the required functions to stay compatible with the XDatasetDef.  3. XDatasetCollectionDef Interface:  This is the interface used to extend any type of data as a Collection, i-e an array of XDatasetDef. This is a slice of any XDatasetDef compatible data.  The interface implements some methods to work on array structure such as Push, Pop, Shift, Unshift and some methods to search data into the array.  The XDatasetCollection type is a simple []DatasetDef with all the implemented methods and should be enough to use for almost all required cases.  1. Overview:  This is a class to compile and keep a Template that can be injected with an XDataSet structure of data, with a metalanguage to inject the data.  The metalanguage is extremely simple and is made to be useful and **really** separate programation from template code (not like other many generic template systems that just mix code and data).  A template is a set of HTML/XML (or any other language) string with a meta language to inject variables and build a final string.  The XCore XTemplate system is based on the injection of parameters, language translation strings and data fields directly into the HTML (Or any other language you need) template.  The HTML itself (or any other language) is a text code not directly used by the template system, but used to dress the data you want to represent in your preferred language.  The variables to inject must be into a XDataSet structure or into a structure extended from XDataSetDef interface.  The injection of data is based on a XDataSet structure of values that can be nested into another XDataSet and XDataSetConnection and so on.  The template compiler recognize nested arrays to automatically make loops on the information.  Templates are made to store reusable HTML code, and overall easily changeable by people that do not know how to write programs.  A template can be as simple as a single character (no variables to inject) to a very complex nested, conditional and loops sub-templates.  Yes. this is a template, but a very simple one without need to inject any data.  Let's go more complex:  Having an array of data, we want to paint it beautifull:  We can create a template to inject this data into it:  2. Create and use XTemplateData:  In sight to create and use templates, you have all those possible options to use:  Creates the XTemplate from a string or a file or any other source:  3. Metalanguage Reference:  3.1 Comments: %-- and --%  You may use comments into your template. The comments will be discarded immediately at the compilation of the template and do not interfere with the rest of your code.  Example:  3.2 Nested Templates: [[...]] and [[]]  You can define new nested templates into your main template A nested template is defined by:  The templteid is any combination of lowers letters only (a-z), numbers (0-9), and 3 special chars: . (point) - (dash) and _ (underline).  The template is closed with [[]].  There is no limits into nesting templates.  Any nested template will inheritate all the father elements and can use father elements too.  To call a sub-template, you need to use &&templateid&& syntax (described below in this document).  Example:  You may use more than one id into the same template to avoid repetition of the same code. The different id's are separated with a pipe |  Important note: A template will be visible only on the same level of its declaration. For example, if you put a subtemplate "b" into a subtemplate "a", it will not be visible by &&b&& from the top level, but only into the subtemplate "a".  3.3 Simple Elements: ##...## and {{...}}  There are 2 types of simple elements. Language elements and Data injector elements (also called field elements).  We "logically" define the 2 type of elements. The separation is only for human logic and template filling, however the language information can perfectly fit into the data to inject (and not use ## entries).  3.3.1 Languages elements: ##entry##  All the languages elements should have the format: ##entry##.  A language entry is generally anything written into your code or page that does not come from a database, and should adapt to the language of the client visiting your site.  Using the languages elements may depend on the internationalization of your page.  If your page is going to be in a single language forever, you really dont need to use languages entries.  The language elements generally carry titles, menu options, tables headers etc.  The language entries are set into the "#" entry of the main template XDataset to inject, and is a XLanguage table.  Example:  With data to inject:  3.3.2 Field elements: {{fieldname}}  Fields values should have the format: {{fieldname}}.  Your fields source can be a database or any other preferred repository data source.  Example:  You can access an element with its path into the data set to inject separating each field level with a > (greater than).  This will take the name of the second hobby in the dataset defined upper. (collections are 0 indexed).  The 1 denotes the second record of the hobbies XDatasetCollection.  If the field is not found, it will be replaced with an empty string.  Tecnically your field names can be any string in the dataset. However do not use { } or > into the names of your fields or the XTemplate may not use them correctly.  We recommend to use lowercase names with numbers and ._- Accents and UTF8 symbols are also welcome.  3.3.3 Scope:  When you use an id to point a value, the template will first search into the available ids of the local level. If no id is found, the it will search into the upper levers if any, and so on.  Example:  At the level of 'data2', using {{appname}} will get back 'DomCore'.  At the level of 'key1', using {{appname}} will get back 'Nested App'.  At the level of 'key2', using {{appname}} will get back 'DomCore'.  At the level of root, 'data1' or 'detail', using {{appname}} will get back an empty string.  3.3.4 Path access: id>id>id>id  At any level into the data array, you can access any entry into the subset array.  For instance, taking the previous array of data to inject, let's suppose we are into a nested meta elements at the 'data1' level. You may want to access directly the 'Juan' entry. The path will be:  The José's status value from the root will be:  3.4 Meta Elements  They consist into an injection of a XDataset, called the "data to inject", into the template.  The meta language is directly applied on the structure of the data array.  The data to inject is a nested set of variables and values with the structure you want (there is no specific construction rules).  You can inject nearly anything into a template meta elements.  Example of a data array to inject:  You can access directly any data into the array with its relative path (relative to the level you are when the metaelements are applied, see below).  There are 4 structured meta elements in the XTemplate templates to use the data to inject:  Reference, Loops, Condition and Debug.  The structure of the meta elements in the template must follow the structure of the data to inject.  3.4.1 References to another template: &&order&&  3.4.1.1 When order is a single id (characters a-z0-9.-_), it will make a call to a sub template with the same set of data and replace the &&...&& with the result. The level in the data set is not changed.  Example based on previous array of Fred's data:  3.4.1.2 When order contains 2 parameters separated by a semicolumn :, then second parameter is used to change the level of the data of array, with the subset with this id. The level in the data set is changed to this sub set.  Example based on previous array of Fred's data:  3.4.1.3 When order contains 3 parameters separated by a semicolumn :, the second and third parameters are used to search the name of the new template based on the data fields to inject.  This is an indirect access to the template. The name of the subtemplate is build with parameter3 as prefix and the content of parameter2 value. The third parameter must be empty.  3.4.2 Loops: @@order@@  3.4.2.1 Overview  This meta element will loop over each itterance of the set of data and concatenate each created template in the same order. You need to declare a sub template for this element.  You may aso declare derivated sub templates for the different possible cases of the loop: For instance, If your main subtemplate for your look is called "hobby", you may need a different template for the first element, last element, Nth element, Element with a value "no" in the sport field, etc.  The supported postfixes are:  When the array to iterate is empty:  - .none (for example "There is no hobby")  When the array contains elements, it will search in order, the following template and use the first found:  - templateid.key.[value]  value is the key of the vector line. If the collection has a named key (string) or is a direct array (0, 1, 2...)  - templateid.first if it is the first element of the array set (new from v1.01.11)  - templateid.last if it is the first element of the array set (new from v1.01.11)  - templateid.even if the line number is even  - templateid in all other cases (odd is contained here if even is defined)  3.4.2.2 When order is a single id (characters a-z0-9.-_), it will make a call to the sub template id with the same subset of data with the same id and replace the @@...@@ for each itterance of the data with the result.  Example based on previous array of Fred's data:  3.4.2.3 When order contains 2 parameters separated by a semicolumn :, then first parameter is used to change the level of the data of array, with the subset with this id, and the second one for the template to use.  Example based on previous array of Fred's data:  3.4.3 Conditional: ??order??  Makes a call to a subtemplate only if the field exists and have a value. This is very userfull to call a sub template for instance when an image or a video is set.  When the condition is not met, it will search for the [id].none template. The conditional element does not change the level in the data set.  3.4.3.1 When order is a single id (characters a-z0-9.-_), it will make a call to the sub template id with the same field in the data and replace the ??...?? with the corresponding template  Example based on previous array of Fred's data:  3.4.3.2 When order contains 2 parameters separated by a semicolumn :, then second parameter is used to change the level of the data of array, with the subset with this id.  Example based on previous array of Fred's data:  If the asked field is a catalog, true/false, numbered, you may also use .[value] subtemplates  3.5 Debug Tools: !!order!!  There are two keywords to dump the content of the data set. This is very useful when you dont know the code that calls the template, don't remember some values, or for debug facilities.  3.5.1 !!dump!!  Will show the totality of the data set, with ids and values.  3.5.1 !!list!!  Will show only the tree of parameters, values are not shown.
Keywords: cache-control|cache-storage|go|golang|internationalization|template-engine|webability|webability-xcore|xcore