Query Caching with PHP & MySQLnd

Query Caching with PHP & MySQLnd

Note: This is part four in our Extending MySQL with PHP's MySQLnd Series

While MySQL has a query cache built in, it has several big problems:

Pros Cons
Easy to use Just enable it in the MySQL config Simplistic It has zero knowledge of your intentions
Transparent No application-level changes are needed Easy to invalidate Any change to a table will invalidate all associated data, even if unnecessary (see: simplistic)
Single threaded because the query cache is single threaded it can actually harm performance

We can solve these issues by using application-level caching, but how can we get the simplicity and ease of use of the query cache without the problems?

This is where the mysqlnd_qc plugin comes in.

Read More

Instances, Classes, and Modules, Oh My!

One of the biggest challenges of object oriented programming in Ruby is defining the interface of your objects. In other languages, such as Java, there is an explicit way to define an interface that you must conform to. But in Ruby, it's up to you.

Compounding with this difficulty is the problem of deciding which object should own a method that you want to write. Trying to choose between modules, class methods, instance methods, structs, and lambdas can be overwhelming.

In this post, we'll look at several ways to solve the Exercism Leap Year problem, exploring different levels of method visiblitiy and scope level along the way.

Leap Year

If you sign up for Exercism.io and start solving Ruby problems, one of the first problems you will look at is called Leap Year. The tests guide you to write a solution that has an interface that works like this:

#=> true

A leap year is defined as a year that is divisible by four, unless it's also divisible by 100. Apparently, centuries aren't leap years. That is, unless they are centuries that are divisible by 400. So there are three rules for leap years:

  1. If it's divisible by 400, it's an exceptional century and is a leap year.
  2. If it's divisible by 100, it's a mundane century and in not a leap year.
  3. Otherwise, if it's divisible by 4, it's a leap year, otherwise it's not.
Read More
Learn about Engine Yard
Try Engine Yard for your Ruby or PHP Apps

Mastering this in JavaScript

JavaScript developers—new and experienced—constantly rate the variable this as a major pain of the language. The this variable, which represents a function’s context, plays an important role in everyday JavaScript programming, so understanding how it works is critical. This blog post gives an overview of this and its many quirks and forms. We will lay out the four ways to set a function’s this value, as well as discuss some common use cases and pitfalls.

What is Context?

But first, what is context in the first place? Think of it like this: if a function is a sentence, the context is the sentence’s subject. It represents who or what the function is about. If a function is creating a new user, the context would be the user instance. The way we access a function’s context is through the variable this.

It seems simple, so why is it so hard to understand? Developers routinely get tripped up by this, and that’s largely due to the fact that we’re used to lexical scoping. With lexical scoping, we can see the value of a variable by finding its assignment in earlier code. So when we want to know what usersCount holds, we can just scroll up and find where that variable is set.

Here’s a visual example:

That’s lexical scoping.

The this variable, however, uses dynamic scoping. We have much less exposure to dynamic scoping than we do to lexical scoping, so naturally it’s more difficult to grasp. With dynamic scoping, variables are set depending on how the host function is called. So the value of this is any given function depends entirely on how and where that function was called in the first place.

Intuition and this

Before getting into the nitty gritty, let’s consider what we already know about this. Most anyone who’s written jQuery code has seen an event binding like this:

$('button').on('click', function() {
    // $(this) refers to the button the was clicked 

When that button is clicked, the callback function runs. We access the $(‘button’) that was clicked inside the callback with $(this). It makes intuitive sense that this would represent the button, because that is the natural subject of the callback function.

The Four Ways to Set a Function’s this Value

Without further ado, here are the 4 ways to set a function’s this value:

  1. Call a method on an object, like object.method()
  2. Pass this in with .call() and .apply()
  3. Use new to make this start out empty
  4. Manually bind a this value with .bind()

If none of the above are used, this is the global scope object (i.e. window in a browser). This is a very bad thing and we should always avoid it by using one of the four rules.

Now, let’s dive into each method.

Read More

Five Ruby Methods You Should Be Using

Five Ruby Methods You Should Be Using

There's something magical about the way that Ruby just flows from your fingertips. why once said "Ruby will teach you to express your ideas through a computer." Maybe that's why Ruby has become such a popular choice for modern web development.

Just as in English, there are lots of ways to say the same thing in Ruby. I spend a lot of time reading and nitpicking people's code on Exercism, and I often see exercises solved in a way that could be greatly simplified if the author had only known about a certain Ruby method.

Here's a look at some lesser-known Ruby methods that solve specific types of problem very well.

Read More

Engine Yard Sponsors Bundler

Engine Yard Sponsors Bundler

Open Source is at the heart of what we and our customers do here at Engine Yard. As a company that founded itself in the roots of the Ruby and Rails communities, we always find the best tools to help foster growth when it comes to building the best applications. In that vein, we are proud to announce our sponsorship of Bundler!

In the dark days, adding gems and managing dependencies in a Ruby application was cantankerous at best. Luckily, a few developers saw the need to fix this. Early in its development, Engine Yard employed Yehuda Katz, Carl Lerche, and André Arko to develop Bundler and help all Ruby developers get out of “dependency hell”.

Read More


Look through our specially curated posts to get focused, in-depth information on a single topic.