Skip navigation

Category Archives: work

This is my custom vim script, based heavily on a script I picked up from DA. The script requires the vim Bundle plugin (Vundle), once installed open a vim window and type ‘BundleInstall’ and the other plugins will be downloaded and installed for the session user. There are a few font and encoding setting that need to be configured, especially when used through putty.

"----------------------------------------------"
" Author: custom vim stuff by jack             "
"----------------------------------------------"
""" Vundle plugin manager {{{
    filetype off                                    " required to init 
    set rtp+=~/.vim/bundle/vundle/                  " include vundle
    call vundle#rc()                                " init vundle
    """ github repos, uncomment to disable a plugin {{{
        Bundle 'chrisbra/SudoEdit.vim'
        Bundle 'ervandew/supertab'
        Bundle 'gmarik/vundle'
        Bundle 'kien/ctrlp.vim'
        Bundle 'lilydjwg/colorizer'
        Bundle 'Lokaltog/vim-powerline'
        Bundle 'Lokaltog/vim-easymotion'
        Bundle 'msanders/snipmate.vim'
        Bundle 'nanotech/jellybeans.vim'
        Bundle 'scrooloose/nerdcommenter'
        Bundle 'scrooloose/nerdtree'
        Bundle 'Townk/vim-autoclose'
        Bundle 'tpope/vim-fugitive'
        Bundle 'tpope/vim-surround'
        Bundle 'trapd00r/x11colors.vim'
        Bundle 'vim-scripts/Align'
        Bundle 'vim-scripts/mru.vim'
        Bundle 'xuhdev/SingleCompile'
        " Requires a compiler or syntax/code checker, see :h Syntastic
        Bundle 'scrooloose/syntastic'
        " Requires ctags installed
        Bundle 'majutsushi/tagbar'
        " Requires a working Ruby-install (but not vim compiled with ruby)
        Bundle 'simmel/vim-pastie'
        " Requires the clang compilator
        Bundle 'Rip-Rip/clang_complete'
        " Requires the python package 'jedi' (also if you're in a
        " no-site-packages virtenv)
        Bundle 'davidhalter/jedi-vim'
""" }}}
""" User interface {{{
    """ Syntax highlighting {{{
        filetype plugin indent on                   " enable ft+plugin detect
        syntax on                                   " syntax highlighting
        set t_Co=256                                " 256-colors
        set background=dark                         " we're using a dark bg
        colors jellybeans                           " select colorscheme
        highlight Normal ctermbg=NONE               " use terminal background
        highlight nonText ctermbg=NONE              " use terminal background
        au BufRead,BufNewFile *.txt set ft=sh       " opens .txt w/highlight
    """ }}}
    """ Interface general {{{
        set cursorline                              " hilight cursor line
        set more                                    " ---more--- like less
        set number                                  " line numbers
        set scrolloff=5                             " lines above/below cursor
        set showcmd                                 " show cmds being typed
        set title                                   " window title
	set titlestring=%t
        set vb t_vb=                                " disable beep and flashing
        set wildignore=.bak,.pyc,.o,.ojb,.,a,       " ignore said files
                       \.pdf,.jpg,.gif,.png,
                       \.avi,.mkv,.so
        set wildmenu                                " better auto complete
        set wildmode=longest,list                   " bash-like auto complete
        """ Encoding {{{
            " If you're having problems with Powerline glyphs you can force
            " UTF-8 if your locale is something else. 
            " WARNING: this will affect encoding used when editing files!
            "
            set encoding=utf-8                    " For Powerline glyphs
        """ }}}
        """ Gvim {{{
            " set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 8
            " set guifont=DejaVu\ Sans\ Mono\ for\ Powerline
            set guifont=DejaVuSansMono-Powerline
            set guioptions-=m                       " remove menubar
            set guioptions-=T                       " remove toolbar
            set guioptions-=r                       " remove right scrollbar
        """ }}}
        """ Powerline {{{
            let g:Powerline_symbols = 'fancy'       " glyphs, req. fontpatch
            let g:Powerline_symbols_override = {
                        \ 'BRANCH': [0x2213],
                        \ }                         " use ∓
            """ Powerline mode names {{{
                let g:Powerline_mode_n  = ' N '
                let g:Powerline_mode_i  = ' I '
                let g:Powerline_mode_R  = ' R '
                let g:Powerline_mode_v  = ' V '
                let g:Powerline_mode_V  = 'V·L'
                let g:Powerline_mode_cv = 'V·B'
                let g:Powerline_mode_s  = ' S '
                let g:Powerline_mode_S  = 'S·L'
                let g:Powerline_mode_cs = 'S·B'
            """ }}}
        """ }}}
    """ }}}
""" }}}
""" General settings {{{
    set hidden                                      " buffer change, more undo
    set history=200                                 " default 20
    set iskeyword+=_,$,@,%,#                        " not word dividers
    set laststatus=2                                " always show statusline
    set listchars=tab:>\                            " > to highlight 
    set list                                        " displaying listchars
    set mouse=a                                     " mouse in all modes
    set nocompatible                                " don't vi-compatible
    set noshowmode                                  " hide mode in cmd-line
    set noexrc                                      " don't use other .*rc(s)
    set nostartofline                               " no goto #1 char in line
    set nowrap                                      " don't wrap lines
    set numberwidth=5                               " 99999 lines
    set ttymouse=xterm2                             " Currently being tested
    """ Folding {{{
        set foldcolumn=0                            " hide folding column
        set foldmethod=indent                       " folds using indent
        set foldnestmax=10                          " max 10 nested folds
        set foldlevelstart=99                       " folds open by default
    """ }}}
    """ Search and replace {{{
        set gdefault                                " default s//g (global)
        set incsearch                               " "live"-search
        set ignorecase                              " case insensitive search
    """ }}}
    """ Matching {{{
        set matchtime=2                             " time to blink match {}
        set matchpairs+=<:>                         " for ci< or ci>
        set showmatch                               " tmpjump to match-bracket
    """ }}}
    """ Return to last edit position when opening files {{{
        autocmd BufReadPost *
            \ if line("'\"") > 0 && line("'\"") <= line("$") |
            \     exe "normal! g`\"" |
            \ endif
    """ }}}
""" }}}
""" Files {{{
    set autochdir                                   " always use curr. dir.
    set autoread                                    " refresh if changed
    set backup                                      " backup curr file
    set backupdir=~/.vim/backup                     " backup director{y,ies}
    set backupext=~                                 " append ~ to backups
    set confirm                                     " confirm changed files
    set noautowrite                                 " never autowrite
    set updatecount=50                              " update swp after 50chars
    """ Persistent undo. Requires Vim 7.3 {{{
        if has('persistent_undo') && exists("&undodir")
            set undodir=~/.vim/undo/                " where to store undofiles
            set undofile                            " enable undofile
            set undolevels=500                      " max undos stored
            set undoreload=10000                    " buffer stored undos
        endif
    """ }}}
""" }}}
""" Text formatting {{{
    set autoindent                                  " preserve indentation
    set backspace=indent,eol,start                  " smart backspace
    "set expandtab                                   " no real tabs
    set nrformats+=alpha                            " incr/decr letters C-a/-x
    set shiftround                                  " be clever with tabs
    set shiftwidth=4                                " default 8
    set smartcase                                   " igncase,except capitals
    set smartindent                                 " see autoindent
    set smarttab                                    " tab to 0,4,8 etc.
    "set softtabstop=4                               " "tab" feels like 
    "set tabstop=4                                   " replace  w/4 spaces
    """ Only auto-comment newline for block comments {{{
        au FileType c,cpp setlocal comments -=:// comments +=f://
    """ }}}
""" }}}
""" Keybindings {{{
    """ General {{{
        " Remap 
        let mapleader=","
        " Quickly edit/source .vimrc
        noremap ve :edit ~/.vimrc
        noremap vs :source ~/.vimrc
        " Yank(copy) to system clipboard
        noremap y "+y
        " Toggle text wrapping
        nmap  w :set invwrap:set wrap? 
        " Toggle folding
        nnoremap   @=(foldlevel('.')?'za':"\")
        vnoremap  zf
        " Delete previous word with C-BS, doesn't work in all terminals
        imap  
        " Bubbling (bracket matching)
        nmap  [e
        nmap  ]e
        vmap  [egv
        vmap  ]egv
        " Move faster
        map  
        map  
        " Treat wrapped lines as normal lines
        nnoremap j gj
        nnoremap k gk
        " Move a line of text using ALT-{j,k}
        nmap  mz:m+`z
        nmap  mz:m-2`z
        " Rebind æøå (Norwegian keys)
        noremap ø :
        noremap å [
        noremap æ ]
        " Split and switch to new pane
        nnoremap d vl             " vertical
        nnoremap s sl             " horizontal
        " We don't need any help!
        inoremap  
        nnoremap  
        vnoremap  
        " Toggle syntax highlight
        map  :if exists("syntax_on")
            \syntax offelsesyntax enableendif
    """ }}}
    """ Plugins {{{
        " Toggle tagbar (definitions, functions etc.)
        map  :TagbarToggle
        " Toggle the NERDTree file browser
        map  :NERDTreeToggle
        " Toggle pastemode, doesn't indent
        set pastetoggle=
        " SingleCompile
        nmap  :SCCompile
        nmap  :SCCompileRun
        call SingleCompile#SetCompilerTemplate('cpp', 'gcc', 'GNU C Compiler',
            \'g++', '-Wall -Wextra -pedantic -O3 -std=c++0x -o $(FILE_TITLE)$',
            \'./$(FILE_TITLE)$')
        call SingleCompile#SetOutfile('cpp', 'gcc', '$(FILE_TITLE)$')
        call SingleCompile#ChooseCompiler('cpp', 'gcc')
        " Toggle Syntastic error list. Probably should be toggleable.
        noremap lo :Errors
        noremap lc :lcl
        " Snipmate remapping
        imap  =TriggerSnippet()
    """ }}}
    """ Highlight characters past 80, toggle with h {{{
        nnoremap h :call ToggleOverLengthHighlight()
        let g:overlength_enabled = 0
        highlight OverLength ctermbg=black guibg=#212121
        function! ToggleOverLengthHighlight()
            if g:overlength_enabled == 0
                match OverLength /\%80v.*/
                let g:overlength_enabled = 1
                echo 'OverLength highlighting turned on'
            else
                match
                let g:overlength_enabled = 0
                echo 'OverLength highlighting turned off'
            endif
        endfunction
    """ }}}
    """ Toggle relativenumber using r {{{
        nnoremap r :call NumberToggle()
        function! NumberToggle()
            if(&relativenumber == 1)
                set number
            else
                set relativenumber
            endif
        endfunction
    """ }}}
""" }}}
""" Misc plugin settings {{{
    " NERDTree
    let g:NERDTreeWinPos = "left"
    let g:NERDTreeHijackNetrw=1
    " TagBar
    let g:tagbar_left = 0
    let g:tagbar_width = 30
    set tags=tags;/
    " Stop CtrlP from recalculating on files on start
    let g:ctrlp_clear_cache_on_exit = 0
    let g:ctrlp_working_path_mode = 'ra'
    let g:ctrlp_root_markers = ['.root', 'Makefile', '.git' ]
    " clang_complete - C++11
    let g:clang_user_options="-std=c++0x"
    " Syntastic
    let g:syntastic_cpp_check_header = 1
    let g:syntastic_cpp_compiler_options = ' -std=c++0x'
    let g:syntastic_mode_map = { 
        \ 'mode': 'passive',
        \ 'active_filetypes': 
            \ ['c', 'cpp', 'javascript', 'perl', 'python', 'sh'] }
    " Automatically remove preview window after autocomplete (clang_complete)
    autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
    autocmd InsertLeave * if pumvisible() == 0|pclose|endif
    " Private pastie (simmel's fork of tpope's vim-pastie with help from garno)
    let g:pastie_private = 1
""" }}}
""" Use ~/.vimrc.local if exists {{{{
    if filereadable($HOME."/.vimrc.local")
        source $HOME/.vimrc.local
    endif
""" }}}

Using memcache to store objects is not a new strategy to reduce database load and improve responsiveness of an application, but it is an often overlooked option due to the ‘extra’ labor of installation and configuration. Hopefully the following information will make this easier for some and me to remember.

Installing and configuring a memcache server on linux:

  1. Download, configure, and compile memcache.
  2. Create a startup script:
    #! /bin/sh
    #
    # chkconfig: - 55 45
    # description: The memcached daemon is a network memory cache service.
    # processname: memcached
    # config: /etc/sysconfig/memcached
    # pidfile: /var/run/memcached/memcached.pid# Standard LSB functions
    #. /lib/lsb/init-functions# Source function library.
    . /etc/init.d/functions
    #
    PORT=11211
    USER=memcached
    MAXCONN=2048
    CACHESIZE=128
    OPTIONS=""
    #
    if [ -f /etc/sysconfig/memcached ];then
    . /etc/sysconfig/memcached
    fi
    #
    # Check that networking is up.
    . /etc/sysconfig/network
    #
    if [ "$NETWORKING" = "no" ]
    then
    exit 0
    fi
    #
    RETVAL=0
    prog="memcached"
    pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
    lockfile=${LOCKFILE-/var/lock/subsys/memcached}
    #
    start () {
    echo -n $"Starting $prog: "
    # Ensure that /var/run/memcached has proper permissions
    if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
    chown $USER /var/run/memcached
    fi
    #
    daemon --pidfile ${pidfile} memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch ${lockfile}
    }
    stop () {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
    rm -f ${lockfile} ${pidfile}
    fi
    }
    # restart
    restart () {
    stop
    start
    }
    # See how we were called.
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status -p ${pidfile} memcached
    RETVAL=$?
    ;;
    restart|reload|force-reload)
    restart
    ;;
    condrestart|try-restart)
    [ -f ${lockfile} ] && restart || :
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
    RETVAL=2
    ;;
    esac
    exit $RETVAL
    
  3. Add the daemon to the service manager, and enable it for runlevel3:
    • chkconfig –add –level 3 memcached
  4. Start the Memcache server:
    • service memcached start

You should now have a functioning memcache server, available on the localhost, port 11211. you can check to see if it’s running with service memcached status. Now, to use it within PHP for example you have to provide the PHP extension for memcache so that you can store and retrieve data.  For this purpose, there is a PECL extension available here: http://pecl.php.net/package/memcache

Building the extension is fairly easy:

  1. wget http://pecl.php.net/get/memcache-3.0.8.tgz
  2. tar -xvzf memcache-3.0.8.tgz
  3. cd memcache-3.0.8
  4. phpize
  5. ./configure && make && make install
  6. Check /etc/php.d/memcached.ini, if it doesn’t exist or correctly point to the compiled extension (*.so) PHP will not be able to communicate with the memcache server.

Now the easy part -> using it. For that, RTFM. http://php.net/manual/en/book.memcache.php

While re-factoring a project for performance and improved consistency, it became necessary to store objects in memory for later use to avoid serialization and anything to do with the session. Memcached to the resue? Somewhat, had to do this one by hand as the environment this project is to be deployed within has a number of custom attributes including custom compiled binaries for PHP, libmemcached, imagick, XCache, gearman, etc. Configuring and compiling memcached couldn’t have been easier:

./configure && make && sudo make install && sudo adduser memcached

(Remember to add the new user memcached will run under, using ‘nobody’ is considered to be unsafe.) Now I needed to make sure the Memcache server would start when runlevel=3, after some experimentation this is the bash script that resulted:

#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# pidfile: /var/run/memcached/memcached.pid
# author: Jack Brain
# Standard LSB functions
#. /lib/lsb/init-functions
# Source function library.
. /etc/init.d/functions
PORT=11211
USER=memcached
MAXCONN=2048
CACHESIZE=128
OPTIONS=''
if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi
# Check that networking is up.
. /etc/sysconfig/network
if [ "$NETWORKING" = "no" ]
then
exit 0
fi
RETVAL=0
prog=”memcached”
pidfile=${PIDFILE-/var/run/memcached/memcached.pid}
lockfile=${LOCKFILE-/var/lock/subsys/memcached}
start () {
echo -n $”Starting $prog: “
# Ensure that /var/run/memcached has proper permissions
if [ "`stat -c %U /var/run/memcached`" != "$USER" ]; then
chown $USER /var/run/memcached
fi
daemon –pidfile ${pidfile} memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P ${pidfile} $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
echo -n $”Stopping $prog: “
killproc -p ${pidfile} memcached
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f ${lockfile} ${pidfile}
fi
}
restart () {
stop
start
}
# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} memcached
RETVAL=$?
;;
restart|reload|force-reload)
restart
;;
condrestart|try-restart)
[ -f ${lockfile} ] && restart || :
;;
*)
echo $”Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart}”
RETVAL=2
;;
esac
exit $RETVAL

With PHP 5.5, the Zend OPcache extension is now enabled by default. This potentially impacts a number of current projects, this post is specifically about caching objects (simpleXMLElement, etc.) in memory without using memcached. Additionally, this also introduces a number of potential conflicts with existing environments which already implement a caching layer such as XCache.

To be continued…

This should be short, and easy. First, the extensions we need:

  • php-mysql
  • php-bcmath
  • php-gd
  • php-magickwand
  • php-mbstring
  • php-mcrypt
  • php-pear-Mail*
  • php-soap
  • php-xml
  • php-xmlrpc

Now the configuration options:

./configure \
--build=x86_64-redhat-linux-gnu \
--host=x86_64-redhat-linux-gnu \
--target=x86_64-redhat-linux-gnu \
--program-prefix= \
--prefix=/usr \
--exec-prefix=/usr \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconfdir=/etc \
--datadir=/usr/share \
--includedir=/usr/include \
--libdir=/usr/lib64 \
--libexecdir=/usr/libexec \
--localstatedir=/var \
--sharedstatedir=/var/lib \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--cache-file=../config.cache \
--with-libdir=lib64 \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--disable-debug \
--with-pic \
--disable-rpath \
--with-pear \
--with-bz2 \
--with-freetype-dir=/usr \
--with-png-dir=/usr \
--with-xpm-dir=/usr \
--enable-gd-native-ttf \
--without-gdbm \
--with-gettext \
--with-gmp \
--with-iconv \
--with-jpeg-dir=/usr \
--with-openssl \
--with-pcre-regex=/usr \
--with-zlib \
--with-layout=GNU \
--enable-exif \
--enable-ftp \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg \
--with-kerberos \
--enable-shmop \
--enable-calendar \
--with-libxml-dir=/usr \
--enable-xml \
--with-apxs2=/usr/sbin/apxs \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-soap \
--with-xmlrpc \
--with-tidy=/usr \
--with-gd \
--enable-dom \
--disable-dba \
--without-unixODBC \
--enable-pdo \
--disable-xmlreader \
--disable-xmlwriter \
--disable-phar \
--enable-fileinfo \
--enable-json \
--without-pspell \
--disable-wddx \
--with-curl \
--disable-posix \
--disable-sysvmsg \
--disable-sysvshm \
--disable-sysvsem \
--enable-mbstring \
--enable-bcmath \
--with-mcrypt \
--with-freetype-dir=/usr \
--enable-opcache

Make and install:
sudo make && make install

In skipped make test here for brevity. If you’re in with El Duderiono then you might want to spend the time to check your work. Production is not a land for the lazy.