Page 2 of 3

Clear APC cache in Apache using PHP

The following code will clear the apc cache using PHP

 

DB synchronization- local and remote DB using SymmetricDS

We can sync local DB (which is running in each physical store) with remote DB (centralized DB) which is running in web.

Complexity
– Local DB always not connected with internet.
– We can not use mysql replication (two way sync) because it requires that both DB
should run in web. otherwise, remote DB not understand and sync with local DB.
– Even, if we are achieving i.e two way sync in some other way, all remote DB data going to sync with local DB. This is not good idea to maintain all the store details in all places.
– May some tables are one-way sync from remote DB to local DB vice versa.
– May some tables are condition based sync from remote DB to local DB (eg): Products table

Pre Condition
– Assume that we have mysql running in local & remote server
– We had created database in both places (local & remote server) which we are going to sync
– We have created tables in both Database (local & remote server)

SymmetricDS
– SymmetricDS is best fit for this requirements after reviewing number of open source tools like SqlYog, Todd, MySql Benchmark..etc..,
– It is developed by Java but it can run as standalone application in server. Means, no need tomcat or weblogic servers.
– SymmetricDS should be run in both places (local & remote)
– It is listening and syncing all the changes based on the configuration.

System Requirements
– SymmetricDS is written in Java 5 and requires a Java SE Runtime Environment (JRE) or Java SE Development Kit (JDK) version 5.0 or above.
– It supports MySQL version 5.0.2 and above

Installing SymmetricDS
– Download the source from http://sourceforge.net/projects/symmetricds/files/
– Unzip the file in any directory you choose. This will create a symmetric-ds-3.x.x directory, which corresponds to the version you downloaded.
– Please install the same version in both local & remote system.

SymmetricDS configuration for Remote
– Copy the [samples/corp-000.properties] properties files for the server into the [engines] directory of the SymmetricDS installed
– Change the property file name to [server.properties] instead of [symmetric.properties]
– Do the changes in the following property file

– Import configuration tables that is needed for symmetricDS by executing the following
command using CLI. It will create 41 tables with prefix [sym_]

– –Two way sync configuration (or) Default router
The simplest router is a router that sends all the data that is captured by its
associated triggers to all the nodes that belong to the target node group defined in
the router. A router is defined as a row in the ROUTER table. It is then linked to
triggers in theTRIGGER_ROUTER table.

The following SQL statement defines a router that will send data from the ‘corp’ group
to the ‘store’ group.

One way sync configuration (or) Column Match Router
Consider a table that needs to be routed to only nodes in the target group whose
STORE_ID column matches the external id of a node. The following SQL statement will
insert a column router to accomplish that.

SymmetricDS configuration for Local
– Copy the [samples/store-001.properties] properties files for the local into the [engines] directory of the SymmetricDS install
– change the property file name to [client.properties] instead of [symmetric.properties]
– Do the changes in the following property file

– Please apply [store id] from DB table to [external.id]
– set the following property in the client.properties file to specify where the server node
can be contacted

Starting SymmetricDS
Database setup and configuration is now completed. Time to put SymmetricDS into action.
We will now start both SymmetricDS nodes and observe the logging output.
– Start SymmetricDS by executing:

— The server and client nodes both start up. The server node creates all the
triggers that were configured by the sample configuration. It listens on port
8080 for synchronization and registration requests.
— The client node server starts up and uses the auto-creation feature to create
the SymmetricDS system tables. It begins polling the server node in order to
register. Since registration is not yet open, the client node receives an
authorization failure (HTTP response of 403).

– Registering a Node
Open registration for the client node server by executing:

— The registration is now opened for a node group called “store” with an external
identifier of “1”. This information matches the settings in client.properties for
the client node. Each node is assigned to a node group and is given an
external ID that makes sense for the application. In this tutorial, we have retail
stores that run SymmetricDS, so we named our node group “store” and we
used numeric identifiers starting with “1”. More information about node groups
will be covered in the next chapter.
— Watch the logging output of the client node to see it successfully register with
the server node. The client is configured to attempt registration once per
minute. Once registered, the server and client are enabled for synchronization!

– Sending an Initial Load
— Next, we will send an initial load of data to our store (that is, the client node),
again using the server node administration feature.
— Open a command prompt and navigate to the samples subdirectory of your
SymmetricDS installation.
— Send an initial load of data to the client node server by executing:

— With this command, the server node queues up an initial load for the client
node that will be sent the next time the client performs its pull. The initial load
includes data for each table that is configured for synchronization.
— Watch the logging output of both nodes to see the data transfer. The client is
configured to pull data from the server every minute.

Conclusion
The above steps are needed to be followed for syncing local & remote DB. The following point
are very useful for us.
– [external.id] should be unique for each store configuration and it should not be
changeable and it must be a store id which is created by Administrator in store table.
This store id is a reference for one way sync.
– Data will not sync with remote DB if server not started in local but data will be sync
after starting local symmtricDS server viceversa.
– We need to add entry in [sym_router] & [sym_trigger_router] for every new table
that is used by our application.
– For every new store, need to [registering a node] with remote server.
– For every new store, need to recognize [initial load] that is sent by remote server.
then only, sync will happen.

 

Execute script in each partial loads in Angularjs

By default, Angularjs is not loading script if we put it in partial HTML. Also, it is compiling the HTML  once template is loaded. So subsequent request template will be loaded from browser cache instead of from server.

In some cases, we want to execute some codes whenever partials loaded (for eg: Tracking page count code, putting Google Analytics code or any other tracking code).

So the solution is to call Jquery before loading Angularjs. This way, Angular in-built JQlite is detached and Angular will use full fledged of external Jquery.

index.html

The following partials are used in this demo.
– home.html
– projects.html
– services.html
– downloads.html
– about.html
– contact.html

Each file contains inline script with console log and it will be printed whenever the partials are loaded.

Demo: http://www.amasik.com/demo/angularjs/exec_inline_script/
Enable developer tool and check the console log.

loading order of files with dependency using requirejs

The following demo demonstrates the loading order of files with dependency. Below is my project directory structure. JS folder contains number of js files (required by project).

– js
–script1.js
–script2.js
–script3.js
–dependent1.js
–dependent2.js
–core
—require.js
—main.js (this is having the configuration)
—app.js (init the loading process)
-index.html

We are going to load the js files in the following order.

-script3.js
-dependent2.js
-script2.js
-dependent1.js
-script1.js

index.html

main.js

app.js

 Demo:

Open in new tab with browser developer tool and track the loading order

http://www.amasik.com/demo/requirejs/index.html

IE confusing browser & document modes

Often developers confusing regarding the following IE modes. Simply I explained below what does, each one.

Browser Mode:
It is an identifier of browser version which is used in code.
Client Side:
Javascript: navigator.userAgent
Server Side:
PHP: $_SERVER[‘HTTP_USER_AGENT’];

Document Mode:
This is only renedering the page using IE engine.

Compatibility Mode:
This is changing the browser mode and document mode to lower version i.e IE7. This is mostly creating a problem in production environment in IE.

Edge Mode:
> Developer can force document mode to Edge. Means, IE automatically changed to highest version of browser.
> It is very useful for fixing the compatability mode issue.
> Anyway, developer can not force browser mode. So still your apps gets problem if you ae checking with IE versions in code level.

HTML:
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
PHP:
header(“X-UA-Compatible: IE=Edge”);

Quirks Mode:
> If you are not mention doctype, then your browser gets old browser engine and some of the functionality will not works fine in your app.
> Use standart doctype <!DOCTYPE html>

Conclusion:
> Use doctype <!DOCTYPE html>
> Add X-UA-Compatible to edge
> Both mode (Browser mode & Document mode) should same in your testing scenarios if edge mode not enabled.

Angularjs simple front-end apps using ngRoute

Angularjs is very flexible for Single Page Application(SPA). This post explains you that how we can perform navigating between one view to another view using ngRoute module and used Bootstrap for layouts. Please go to this post If you are interested this demo using ui-router module.

Sample:

http://www.amasik.com/demo/angularjs/front-end-ng-route/index.html

Module Source: https://code.angularjs.org/1.3.2/angular-route.min.js

Dependent Module: ngRoute

Router Provider:

Default View Config:

Router Config:

View Directive: ng-view

Templates: Used In-line templates home.html, projects.html..etc., (Refer code)

Link Directive : href=”#/home”  (use html href directly)

Source Code:

HTML

js

 

Angularjs simple front-end apps using ui-router

Angularjs is flexible for Single Page Application(SPA). This post explains you that how we can perform navigating between one view to another view using ui-router module and Bootstrap for layouts. Please go to this post If you are interested this demo using ngRoute module.

Sample:

http://www.amasik.com/demo/angularjs/front-end-ui-router/index.html

Module Source: http://angular-ui.github.io/ui-router/release/angular-ui-router.min.js

Dependent Module: ui.router

Router Provider:

Default View Config:

Router Config:

View Directive: ui-view

Templates: Used In-line templates home.html, projects.html..etc., (Refer code)

Link Directive : ui-sref=”home”  (here ‘home’ is a state, not url)

Full Source:

HTML

JS

 

Angularjs ngRoute vs ui-router

Router is playing important role in Single Page Application (SPA). It is getting & applying the Partials/Views/Templates into corresponding places. Also, using this technique, navigation between  one view to another view is performed.

ngRoute is a angular core module which is good for basic scenarios. I believe that they will add more powerful features in upcoming releases.

URL: https://docs.angularjs.org/api/ngRoute

> Ui-router is a contributed module which is overcome the problems of ngRoute. Mainly Nested/Complex views.

URL: https://github.com/angular-ui/ui-router

Some of the difference between ui-router and ngRoute

Open in new tab for better results:

http://www.amasik.com/demo/angularjs/uiRouter-vs-ngroute/index.html

Sample  App:

ngRoute:

http://www.amasik.com/angularjs-simple-front-end-apps-using-ngroute/

ui-router:

http://www.amasik.com/angularjs-simple-front-end-apps-using-ui-router/

Angularjs directive scopes

The significance feature of Angularjs is Directive. Angularjs is HTML enhanced for web apps. Means, developer can introduce new HTML element (eg: <datatable>) or new attribute ( goTo=””) whenever needed and write a script for their behavior. We should know about the scope of directives. So that we can write Directive efficiently.

I assume that you have some prior knowledge regarding directive. This post explains the type of scope can be used for directives.

1. scope: false (Default)

This is default behavior. you can get the parent scope inside directive and this scope will be act as two-way data binding. Means, if you change the scope inside directive, then it will be changed in parent.

Example: (Check country scope/model)

Code:

2. scope: true (Inherit scope)

You can get the parent scope inside directive and this scope will be act as one-way data binding. Means, if you change the scope inside directive, then it will not be changed in parent.

Example: (Check country scope/model)

Code:

 

3. scope:{} (Isolated scope)

You can pass particular scopes inside directive through attributes and you may decide whether these scopes will be act as two-way data binding (=) or one-way data binding (@).

Example: (two-way data binding) (Check country scope/model)

Code:

 

Example: (one-way data binding) (Check country scope/model)

Code:

 

PDO right way to execute queries

We can execute queries in the following ways using PDO . I preferred to use prepare method.
> Query Method
> Execute Method
> Prepare Method

Here we can take an example to delete 3rd record from departments table.

Establish a DB connection.
$conn = new PDO(‘mysql:host=localhost;dbname=demo’, ‘username’, ‘password’);

Query Method
Syntax: $conn->query();
Parameter: SQL Statement
Success: PDOStatement
Failure: false
Example:

Dis-advantage:
We need to care about SQL injection attacks. means if $id contains any vulnerability.

Execute Method
Syntax: $conn->exec();
Parameter: SQL Statement
Success: Number of rows
Failure: false
Example:

Dis-advantage:
We need to care about SQL injection attacks. means if $id contains any vulnerability.

Prepare Method
Syntax: $conn->prepare();
Parameter: SQL Statement
Success: PDOStatement
Failure: false
Example:

Advantages:
Here we can not pass the value directly in SQL statement instead use named parameters (:name or ?). It prevent SQL injection attacks

© 2017 Asik Blog

Theme by Anders NorenUp ↑