易栈网-膘叔

浏览模式: 标准 | 列表 | 图文
琐事杂记

琐事杂记

辛苦写了上千个字。出门走了一次又消失了。。。。郁闷,自动保存居然也没有保存下来。。

说了几件事情。。。

1、工作,目前状态求职中。离职是我的选择。因为公司注册在浙江,社会保险等都是交在浙江的,对于我想在上海发展不太有利,所以我选择了辞职,想找家能够在上海交险交金的工作。很多人都认为我现在这样的做法很荒谬但我想,做任何事都是有得有失的吧?

2、小孩,小孩的湿疹好象又反复了。今天带他去了华山医院看了一下,在拿出以前的葯给医生看时,医生说,你们怎么可以用XXXX这个药?这个小孩不能用的。。后来仔细看了一下说明,上面写着:不能给小孩使用,过敏体质慎用。可是儿童医院还是开出了这种葯,我真的很无语。说到湿疹,我得说一下抱歉,因为家庭因为小孩的原因,我拒绝了千乘的职位,小家伙经常生病,实在放心不下。

3、自己的代码又在写了,以前一直提的sablog YII重构,目前已经正式开始写了。当然还只是初步,这次的模版用的是wordpress的模版改的。感觉改起来问题不大,5%完成中,有的等了,初期先把前台搞定。。

zend newsletter

收邮件的时候,收到了这封zend framework更新的信息,果然不出我所料,ZF要升级了,但究竟什么 时候能够出来,还要等啊

官方发出一条信息关于zend framework2模式的网络会议。它说:

Zend Framework 1 grew organically. An effort behind version 2.0 was to identify common themes and patterns, and to unify these in order to provide common low-level patterns to use everywhere.

Presented by Matthew Weier O'Phinney, this webinar will serve as an overview of these patterns, identifying them, and discussing the interfaces involved and the concrete use cases we plan to ship in ZF2. Matthew will also show how you might create your own implementations and slip-stream them into your application. Amongst the patterns discussed will be events, brokers, and dispatchers.
如果有兴趣的话还可以参加讨论。。。March 30,不知道讨论结束后会有怎么样的一个结果。比如2的模式是否与1一样,不审会有一些其他的构思,需要期待了

Java and C# Comparison

会java不会C#,或者会C#不会java?没关系,这里有对比。。。
看一下hello world的对比先。。

package hello;

public class HelloWorld {
public static void main(String[] args) {
String name = "Java";

// See if an argument was passed from the command line
if (args.length == 1)
name = args[0];

System.out.println("Hello, " + name + "!");
}
}

C#呢?

using System;

namespace Hello {
public class HelloWorld {
public static void Main(string[] args) {
string name = "C#";

// See if an argument was passed from the command line
if (args.Length == 1)
name = args[0];

Console.WriteLine("Hello, " + name + "!");
}
}
}

是不是很像吗?这里还有更多。。。http://www.harding.edu/fmccown/java_csharp_comparison.html。嗯,官方是这么说的:

This is a quick reference guide to highlight some key syntactical differences between Java and C#.
This is not a complete overview of either language. Hope you find this useful!

除了这两种语言的对比,该页还有一个链接,是VB.net与C#的对比,对于只会vb.net的人来说,又是一个学习的途径啊。VB.NET and C# Comparison.

服务器启动memcache

服务器启动memcache

服务器为了性能,已经启用了几个扩展,今天早上,又重新为它加上了memcache这个扩展。
其实以前memcached我早就装了,但memcache扩展我一直没有启用,如果用那个纯PHP的接口,也还是可以使用的。现在用上自带的标准接口,是为了未来的yii框架做打算(不过我也只开了很少的64M的缓冲区,对于我这样的小网站,足够了)

在ubuntu上安装memcache扩展是非常方便的。apt一下就全回来了。只是apache+php+mysql是单独配置的。所以还得象网上那些教程一样按顺序操作

1、wget http://pecl.php.net/get/memcache-2.2.3.tgz
2、tar zxvf memcache-2.2.3.tgz 到一个目录里
3、运行/software/php/bin/phpize 会得到一个环境变量(您的系统可能是/usr/local/bin/phpize)
4、./configure –enable-memcache –with-php-config=/usr/local/bin/php-config –with-zlib-dir
如果没有中断,可以直接执行第五步,如果有中断情况,则检查原因
5、make && make install (查看最后输出的路径)
6、到php.ini里查看一下你的Extension指向的路径,把第5步里的路径中的文件copy到extension指向的目录
同时加上extension="memcache.so";
7、/etc/init.d/apachectl restart
OVER

变量的顺序

变量的顺序

我是在一个博客上看到这个代码的。同时也理所当然的认为值是我想象中的那样,然而,现实无情的击破了我的幻想。。代码如下:

var x = 1;
function fn(){
alert(x);
var x = 2;
}
fn();
alert(x);

看到上面的代码,如果清楚全局变量和局部变量的或许都会说,输出1,1,但事实真是这样的吗?COPY出来到浏览器中运行一下,你会发现,第一次alert居然是undefined。。。这是为什么 ?真相呢?真相在哪时在???

让我们翻开这篇博客看一下。。。http://www.cnblogs.com/snandy/archive/2011/03/11/1980399.html ,作者的标题是JavaScript中同名标识符优先级。

一,局部变量先使用后声明,不影响外部同名变量(代码略,就是我上面那一段 )

第一点,函数fn内第一句输出x,x是在第二句才定义的。这在JS中是允许的,这里的允许是指不会出现语法错误程序可以运行。
但在其它语言如C,Java中却是不允许的。变量必须先声明后使用,如

public class Test {
public static void main(String[] args) {
System.out.println(x); // 先使用
int x = 10; // 后声明
}
}

Java中编译器会提示错误,程序无法运行。
第二点,函数fn内的局部变量x不会影响到外部的变量x。即fn内alert输出不是1,而是undefined。

二,形参优先级高于函数名

function fn(fn){
alert(fn);
}
fn('hello'); // --> "hello"
可以看到函数名和形参同名都是fn,输出的是字符串"hello",却不是函数fn的函数体(fn.toString())。

三,形参优先级高于arguments

function fn(arguments){
alert(arguments);
}
fn('hello'); // --> "hello"<br>

arguments对象可以直接在函数内使用,是语言本身提供的一个 特殊标识符 。
这里刚好将形参声明成与其同名。输出可以看到是"hello"而非"[object Object]",即形参arguments覆盖了语言本身提供的真正的arguments。

四,形参优先级高于只声明却未赋值的局部变量

function fn(a){
var a;
alert(a);
}
fn('hello'); // --> "hello"

函数fn形参为a,函数内第一句仅声明局部变量a,却并未赋值。从输出结果是"hello"而非undefined可以看出形参a优先级高于仅声明却未赋值的局部变量a。

五,声明且赋值的局部变量优先级高于形参

function fn(a){
var a = 1;
alert(a);
}
fn('hello'); // --> "1"

函数fn形参为a,函数内第一句仅声明局部变量a,赋值为1。从输出结果是"1"而非"hello"可以看出声明且赋值的局部变量a优先级高于形参a。

六,形参赋值给同名局部变量时

function fn(a){
var a = a;
alert(a);
}
fn('hello');
暂不运行,猜测下结果。如果按照第五点:声明且赋值的局部变量优先级高于形参。那么a将是undefined。但实际上a是"hello",即右a是形参a,左a才是局部变量a。

7f61988c7d3eb784258c0d84359755ad

这里的两个a互不干扰,谁也没覆盖谁。这与刚刚说的赋值的局部变量优先级高于形参又矛盾了。但引擎这样做的确是我们想要的,因为并不希望var a = a后a是undefined。

---EOF---
看了上面的内容,你懂了多少?

FlashCache

FlashCache

在看云风的BLOG的时候,他说游戏处理中IO的开销非常大,甚至一些IO会堵塞1~2秒。细想在WEB应用中,IO的开销就好象没有它那么高了,但一些应用了框架的系统还是会有很高的IO开销。
最早的时候,我是和同事们在考虑,是否系统在起动的时候,就把项目COPY到内存里,然后指定WEB目录为内存中的目录,当然涉及到写操作还是用管道引用到外面。这样的话,程序的IO就可以完全忽略掉了,只是,这个涉及到了,启动顺序和其他一些操作,还得写上不少的shell等,后来就完全搁浅了。这是在linux平台下。
后来,自己有了服务器,用的win平台。于是这时候我也尝试用ramdisk开辟了128M左右的空间用来做WEB应用。我当时的一些Cache的操作也是放在这128M空间里的。只是效果嘛,看不出,因为是自己的小应用,没有访问量。。。。但至少这是可行的(设置ramdisk的时候是关机时保存到磁盘,这样下次开机的时候它还会存在而不会被删除)
云风在博客中说他们用了RDB的结构来处理,即从B(backup)引到一部分数据到R(Ram)里面,然后写的时候,先写到R,然后用脚本増量到D(data)中,这样保证了B只读,D只写,内存R读写一起上。减少了大量的运行时IO开销。
该博客有很多评论,比如有建议用nosql的,有建议其他方法的,比如有个人就介绍了这个flashcache,看看觉得有意思就复制了下来。。。
关于flashcache的原文,在这里:http://www.orczhou.com/index.php/2010/09/flachcache-first-view/。。。

Flashcache是Facebook技术团队的又一力作,最初是为加速MySQL设计的。Flashcache是在Linux层面的,所以任何受磁盘IO困绕的软件或应用都可以方便的使用之。

1. Why Flashcache

随着时间的流逝,网站上的数据一直在不停的积累。如果你经营的只是一个博客的话,这不会是问题,因为10G的空间,大概就够你写一辈子了(如果放在硬盘上,其实一辈子很短)。如果你恰巧在一个快速增长的公司,数据会越来越多,从MB,到GB,再到TB。

如果将这些数据全部放在大容量的SATA、SAS盘上时,会发现性能(响应时间)不够;如果全放在SSD上时,又会发现成本很高。即使公司能够大气 到都放到SSD上,你会发现1TB的数据里面可能只有200G是经常被访问的,300G可能偶尔被访问到,最后剩下的500G可能已经成为历史数据了,几 乎不被访问到,如果全部都放在SSD上有略有浪费。于是就有了Flashcache。

Flashcache一个非常不错的软件(准确的说是一个Linux的模块), 可以动态加载。Flashcache通过在文件系统(VFS)和设备驱动之间新增了一次缓存层,来实现对热门的缓存。Flashcache是另一种缓存, 一般用SSD作为介质的缓存(一般的缓存用的是内存),通过将传统硬盘上的热门数据缓存到SSD上,然后利用SSD优秀的读性能,来加速系统。这个方法较 之内存缓存,没有内存快,但是空间可以比内存大很多。

本文是一个关于Flashcache的初步介绍。

2. 谁适合用Flashcache

数据量很大(例如4TB),热门数据也很大(800GB),不必要或者不舍得全部买内存来缓存。

3. 谁不适合用Flashcache

数据量不大的话,一般Flashcache就没什么用武之地了,内存就可以帮你解决问题了;

不差钱,买内存呗;

另外Flashcache的加入也使得系统的复杂度增加了一层,如果你坚持KISS原则(Keep it simple, Stupid!),也可以弃用之。

4. 基本原理图
d3c7e3d7e5f907368c0338925215bf07

上图中,Flashcache将普通的SAS盘(/dev/sda)和一个高速的SSD(/dev/sdb)虚拟成一个带缓存的块设备(/dev/mapper/cachedev)。后续还将会有更多关于Flashcache相关的文章出现,敬请期待。

5. Flashcache是否只是一个过渡产品

对于当前,PCI接口的SSD价格还十分昂贵,而随着时间的推移,价格会降;另外,根据摩尔定律,未来会有更快的设备出现,所以Flashcache是一个“持久的”产品,还只是一个过渡软件,这并不好说。

关注未来新技术的发展吧:)

参考:

1. Facebook / Flashcache

2. Releasing Flashcache of Facebook

3. flashcache-doc.txt

PS:写完整篇文章,最大的收获是:发现人的一生其实很短、很短,可能连1GB都不到。

阅读笔记

阅读笔记

1、DBANOTES中介绍QUORA的一句话:“Quora 大量使用 Amazon EC2 与 S3 服务;操作系统部署的是 Ubuntu Linux,易于部署和管理;静态内容用 Cloudfront.服务分发,图片先传到 EC2 服务器,使用 Pyhon S3 API 处理后后传到 S3。”【Quora 用了哪些技术 ?】。

我想说的是,UbuntuServer看来也越来越被N多人所接受了。原来的那种以centos等为主流的观念也开始慢慢的向ubuntu上转移了,虽然只是很少的一部分,但任何事物都要有个过程的嘛 。

2、来自“为之漫笔”的《NetBeans之父Jaroslav Tulach谈软件框架设计》,其中有一些话确实会引起人的共鸣:

  • “API的第一个版本绝对不会完美”
  • “不可能知道使用你的库的所有人”

API第一个版本绝对不会完美,这个其实是所有写代码的人遇到的最常见的问题,问题总是在不断的开发中被发现的,如果你是在不停的修正BUG,那框架还能继续下去,如果发现问题就重写,那框架永远也完成不了。。。。

3、张鑫旭的博客上有些CSS和JQuery的插件不错。比如这个:jQuery powerFloat万能浮动层下拉层插件。地址在:http://www.zhangxinxu.com/wordpress/?p=1328,还有一个弹出窗框架也挺好。一些简单的应用完全足够了。。

三八节快乐

三八节快乐

三八节看起来不象是我们男性过的节日,但事实上,在三八节,我真的要感谢很多人。。。
远的就不说了,以前那些老师啥的很多都是女性的。

就从身边吧,感谢我的母亲,感谢我的岳母,感谢我的妻子。这三个人是我最想表达感谢之意的。

母亲生我养我,这是毋庸置疑的,当然得感谢。

岳母和岳父每天辛苦的帮我们照顾小朋友,我父母又不在上海,岳父母承担的压力就大了,几乎是天天在照顾着小孩(几乎每个小孩都是自己爷爷奶奶或者外公外婆带大的吧。),我和妻子回家又晚,她们辛苦极了。

最后感谢我的妻子,除了因为爱情,还有小朋友啊。虽然我更喜欢小姑娘多一点,但小家伙也是个很可爱的人呢。

mysql.7z

Best MVC Practices

Best MVC Practices

这是一篇来自Yii官方的文章,写在了1.1.6的new fetures里面,值得一看,最起码,你在看完后,下次被人面试的时候,问到什么 是MVC,你可以有回答的东西了。

不多说,上原文,内容也不复杂,所以我就不翻译了(其实是我翻译不来,将就着看看可以,可是要翻译成人人都能看得懂的内容就难了)。。。

原文来自于:http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices,目前官方也确实没有中文版。

Although Model-View-Controller (MVC) is known by nearly every Web developer, how to properly use MVC in real application development still eludes many people. The central idea behind MVC is code reusability and separation of concerns. In this section, we describe some general guidelines on how to better follow MVC when developing a Yii application.

To better explain these guidelines, we assume a Web application consists of several sub-applications, such as

  • front end: a public-facing website for normal end users;
  • back end: a website that exposes administrative functionality for managing the application. This is usually restricted to administrative staff;
  • console: an application consisting of console commands to be run in a terminal window or as scheduled jobs to support the whole application;
  • Web API: providing interfaces to third parties for integrating with the application.

The sub-applications may be implemented in terms of modules, or as a Yii application that shares some code with other sub-applications.

1. Model

Models represent the underlying data structure of a Web application. Models are often shared among different sub-applications of a Web application. For example, a LoginForm model may be used by both the front end and the back end of an application; a News model may be used by the console commands, Web APIs, and the front/back end of an application. Therefore, models

  • should contain properties to represent specific data;

  • should contain business logic (e.g. validation rules) to ensure the represented data fulfills the design requirement;

  • may contain code for manipulating data. For example, a SearchForm model, besides representing the search input data, may contain a search method to implement the actual search.

Sometimes, following the last rule above may make a model very fat, containing too much code in a single class. It may also make the model hard to maintain if the code it contains serves different purposes. For example, a News model may contain a method named getLatestNews which is only used by the front end; it may also contain a method named getDeletedNews which is only used by the back end. This may be fine for an application of small to medium size. For large applications, the following strategy may be used to make models more maintainable:

  • Define a NewsBase model class which only contains code shared by different sub-applications (e.g. front end, back end);

  • In each sub-application, define a News model by extending from NewsBase. Place all of the code that is specific to the sub-application in this News model.

So, if we were to employ this strategy in our above example, we would add a News model in the front end application that contains only the getLatestNews method, and we would add another News model in the back end application, which contains only the getDeletedNews method.

In general, models should not contain logic that deals directly with end users. More specifically, models

  • should not use $_GET, $_POST, or other similar variables that are directly tied to the end-user request. Remember that a model may be used by a totally different sub-application (e.g. unit test, Web API) that may not use these variables to represent user requests. These variables pertaining to the user request should be handled by the Controller.

  • should avoid embedding HTML or other presentational code. Because presentational code varies according to end user requirements (e.g. front end and back end may show the detail of a news in completely different formats), it is better taken care of by views.

2. View

Views are responsible for presenting models in the format that end users desire. In general, views

  • should mainly contain presentational code, such as HTML, and simple PHP code to traverse, format and render data;

  • should avoid containing code that performs explicit DB queries. Such code is better placed in models.

  • should avoid direct access to $_GET, $_POST, or other similar variables that represent the end user request. This is the controller's job. The view should be focused on the display and layout of the data provided to it by the controller and/or model, but not attempting to access request variables or the database directly.

  • may access properties and methods of controllers and models directly. However, this should be done only for the purpose of presentation.

Views can be reused in different ways:

  • Layout: common presentational areas (e.g. page header, footer) can be put in a layout view.

  • Partial views: use partial views (views that are not decorated by layouts) to reuse fragments of presentational code. For example, we use _form.php partial view to render the model input form that is used in both model creation and updating pages.

  • Widgets: if a lot of logic is needed to present a partial view, the partial view can be turned into a widget whose class file is the best place to contain this logic. For widgets that generate a lot of HTML markup, it is best to use view files specific to the widget to contain the markup.

  • Helper classes: in views we often need some code snippets to do tiny tasks such as formatting data or generating HTML tags. Rather than placing this code directly into the view files, a better approach is to place all of these code snippets in a view helper class. Then, just use the helper class in your view files. Yii provides an example of this approach. Yii has a powerful CHtml helper class that can produce commonly used HTML code. Helper classes may be put in an autoloadable directory so that they can be used without explicit class inclusion.

3. Controller

Controllers are the glue that binds models, views and other components together into a runnable application. Controllers are responsible for dealing directly with end user requests. Therefore, controllers

  • may access $_GET, $_POST and other PHP variables that represent user requests;

  • may create model instances and manage their life cycles. For example, in a typical model update action, the controller may first create the model instance; then populate the model with the user input from $_POST; after saving the model successfully, the controller may redirect the user browser to the model detail page. Note that the actual implementation of saving a model should be located in the model instead of the controller.

  • should avoid containing embedded SQL statements, which are better kept in models.

  • should avoid containing any HTML or any other presentational markup. This is better kept in views.

In a well-designed MVC application, controllers are often very thin, containing probably only a few dozen lines of code; while models are very fat, containing most of the code responsible for representing and manipulating the data. This is because the data structure and business logic represented by models is typically very specific to the particular application, and needs to be heavily customized to meet the specific application requirements; while controller logic often follows a similar pattern across applications and therefore may well be simplified by the underlying framework or the base classes.