Memcached vs APC vs Varnish

Alternative PHP Cache (APC)
APC provides two caching mechanisms.

APC caches the opcode generated during the PHP execution cycle and then later will be able to avoid loading and parsing PHP files repeatedly.
APC also provides a key-value, object cache for PHP applications in shared memory.

Memcached is a distributed, key-value, object cache in memory. This is similar to the object cache provided by APC but there are some important differences. It’s in-memory, while APC’s object cache is in shared memory. This will make Memcached faster, but will also require the memory allocation for it’s storage. The other major difference is that Memcached is distributed. This means that it runs across multiple servers. For example, if you’re load-balancing your application. Generally, the need for a distributed object cache is why you would use Memcached.

Varnish is a caching HTTP reverse proxy. The reverse proxy part means that it sits between your application and the outside world. Visiting your domain will actually connect to Varnish. Varnish will then make the corresponding request to your application and then deliver it to the client. It will cache the results of these requests based on a configuration file you can write in the Varnish Configuration Language.

Varnish is neither an opcode cache like APC’s opcode cache component, nor an object cache like those in APC or Memcached. Varnish operates outside of your application and caches the entire HTTP response such as the whole HTML document returned by your application.

Varnish is extremely effective because it will cache the final resulting HTML document after all PHP or other server side processing is done. This means that when varnish delivers a page from cache, it avoids running the PHP code at all and consequently any database queries involved in generating the document. Delivering a cached page from Varnish is like delivering a static HTML file.

Static Concept

A static variable is one that’s associated with a class, not objects of that class.

Static is an access qualifier that limits the scope but causes the variable to exist for the lifetime of the program. This means a static variable is one that is not seen outside the function in which it is declared but which remains until the program terminates. It also means that the value of the variable persists between successive calls to a function.

void fun() { 
    static int fnvalue=0;//Executed once 
    printf(" \n%d",fnvalue++);// Value changed retains for next call 

By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::. A static member function can only access static data member, other static member functions and any other functions from outside the class. Static member functions have a class scope and they do not have access to the this pointer of the class.

Depth First Search vs Breadth First Search

Karumanchi (pg 216)

1) Visit a node, then one of its child, then one of its child, till you reach the leaf. Then backtrack and visit other childs.
2) lower memory requirements since its not required to store all child pointers at each level.
3) If data being looked for has more probability of being at the bottom of the tree, then DFS is a good fit.
4) DFS works off a stack
5) Example: Good for solving puzzles such as mazes, finding strongly connected components (Finding Strongly Connected Components of a graph A directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex).
6) Simple algo is recursive

1) more memory than DFS
2) If data being looked for has more probability of being at top of the tree then BFS is a good fit.
3) It visits all vertices at level 1 before proceeding ahead. Then it visits all vertices at level 2. Is similar to level order traversal of trees.
4) BFS works off a queue
5) Example: shortest path,
6) simple algo is non recursive (with a while loop)

Questions on Testing

1) Difference between functional testing and unit testing
Unit Tests are written from a programmers perspective. They are made to ensure that a particular method (or a unit) of a class performs a set of specific tasks.

Functional Tests are written from the user’s perspective. They ensure that the system is functioning as users are expecting it to.

2) What is TDD (Test Driven Development)

3) What is a “mock” and “stub”?
They are both fake objects.

– Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ‘sent’, or maybe only how many messages it ‘sent’.
– State Testing

Setup – Prepare object that is being tested and its stubs collaborators.
Exercise – Test the functionality.
Verify state – Use asserts to check object’s state.
Teardown – Clean up resources.

– objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
– Behavioral Testing
– Test lifecycle with mocks:

Setup data – Prepare object that is being tested.
Setup expectations – Prepare expectations in mock that is being used by primary object.
Exercise – Test the functionality.
Verify expectations – Verify that correct methods has been invoked in mock.
Verify state – Use asserts to check object’s state.
Teardown – Clean up resources.

Both mocks and stubs testing give an answer for the question: What is the result?

Testing with mocks are also interested in: How the result has been achieved?

Mocks vs Stubs = Behavioral testing vs State testing


I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They would not do anything fancy and you would have already written the stubbed code for it outside of your test.


A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.


Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify.


The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.

Stub – override methods to return hard-coded values, also referred to as state-based.
Example: Your test class depends on a method Calculate() taking 5 minutes to complete. Rather than wait for 5 minutes you can replace its real implementation with stub that returns hard-coded values; taking only a small fraction of the time.

Mock – very similar to Stub but interaction-based rather than state-based. This means you don’t expect from Mock to return some value, but to assume that specific order of method calls are made.
Example: You’re testing a user registration class. After calling Save, it should call SendConfirmationEmail.

Language Comparision


var colors = “green”;

$colors = “green”;


var colors ;
colors = [‘white’,’black’,’brown’];


var colors = Array(‘white’,’black’,’brown’);


var hotel = {
name: ‘Super8’,
rooms: 40,
booked: 25

checkAvailability = function(){
return this.rooms – this.booked;


var hotelName =;
var roomsFree = hotel.checkAvailability();